Copy + Paste on Right click context menu

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Copy + Paste on Right click context menu

05 Aug 2019, 08:32

I am making a right click context menu and want to include copy and paste like the standard windows context menu has. While the below code works, the paste is not instant like windows menu.

Notice I activate notepad before sending the keystrokes to copy or paste. Window menu does not steal focus from the program you right click on.

Does anyone know how to add in a proper copy and paste to a context menu?

Thanks in advance! :)
Vh_

Code: Select all

#SingleInstance,Force

TrayTip, Context Menu, Script is Active., 5, 1
Menu, Tray, Add, Copy
Menu, Tray, Add, Paste
Menu, Tray, NoStandard ;removes default options
Menu, Tray, Add,  ;creates a seperator
Menu, Tray, Add, Exit
return


#IfWinActive, Untitled - Notepad
RButton::
Menu, Tray, Show  ; right mouse and windows
return

Copy:
WinActivate, Untitled - Notepad
Send, {AppsKey}c
Return

Paste:
WinActivate, Untitled - Notepad
;ControlFocus, %WhatField%, MyProgram
Send, {AppsKey}p
Return


Exit:
ExitApp
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy + Paste on Right click context menu

06 Aug 2019, 10:00

Hi,
although I'm not completely satisfied with my solution, it works - and even better, it works in any program.

Code: Select all

#SingleInstance,Force

TrayTip, Context Menu, Script is Active., 5, 1
Menu, Tray, Add, Copy
Menu, Tray, Add, Paste
Menu, Tray, NoStandard ;removes default options
Menu, Tray, Add,  ;creates a seperator
Menu, Tray, Add, Exit
return


RButton::
Menu, Tray, Show  ; right mouse and windows
return

Copy:
Send {LWin}
Sleep 75
Send {LWin}
Send ^c
Return

Paste:
Send {LWin}
Sleep 75
Send {LWin}
Send ^v
Return


Exit:
ExitApp
:eh: :think:
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy + Paste on Right click context menu

06 Aug 2019, 10:10

Hi again,
I found a better solution.
This one will keep track of the active window on right-click and willl activate it again after the user clicks on a menu-entry.

Just replace the above with this:

Code: Select all

RButton::
WinGetTitle, WindowCache, A
Menu, Tray, Show
return

Copy:
WinActivate, %WindowCache%
Send ^c
Return

Paste:
WinActivate, %WindowCache%
Send ^v
Return
Hope I could help!
:eh: :think:
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Copy + Paste on Right click context menu

06 Aug 2019, 20:48

Sir Teddy the First wrote:
06 Aug 2019, 10:10
Hi again,
I found a better solution.
This one will keep track of the active window on right-click and willl activate it again after the user clicks on a menu-entry.

Just replace the above with this:

Code: Select all

RButton::
WinGetTitle, WindowCache, A
Menu, Tray, Show
return

Copy:
WinActivate, %WindowCache%
Send ^c
Return

Paste:
WinActivate, %WindowCache%
Send ^v
Return
Hope I could help!
Thanks for the reply! I did try something similar. It works, that's the main goal! I'd just love to have it instant like windows, but it's a luxury to have our custom menu as it is, I suppose. :) :) Thanks again!
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy + Paste on Right click context menu

07 Aug 2019, 03:34

Vh_ wrote:
06 Aug 2019, 20:48
I'd just love to have it instant like windows ...
I do not quite understand that. When I run my code it is instant, there is absolutely no delay or anything.

What exactly does it do when you say "it is not instant"? :wtf:
:eh: :think:
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Copy + Paste on Right click context menu

22 Aug 2019, 07:32

Sir Teddy the First wrote:
07 Aug 2019, 03:34
Vh_ wrote:
06 Aug 2019, 20:48
I'd just love to have it instant like windows ...
I do not quite understand that. When I run my code it is instant, there is absolutely no delay or anything.

What exactly does it do when you say "it is not instant"? :wtf:
Hey! I ended up removing the delay with ControlGetFocus and ControlFocus. It was WinActivate that made it have such a lag. :)

Thanks for all your help! :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, jameswrightesq and 290 guests