autohotkey: simulate mouse press and hold - windows

Using autohotkey, I am trying to do make:
pressing and holding LCtrl & LShift behave like pressing and holding RCtrl + Left mouse button
releasing LCtrl & LShift behave like releasing RCtrl + Left mouse button
ideally the order in which the keys are pressed should not matter.
What I have at the moment is:
LCtrl & LShift::
If (A_PriorHotKey = A_ThisHotKey) ;these are built in variables
return
Send {RCtrl Down}
MouseClick, left,,, 1, 0, D ; Hold down the left mouse button.
return
LCtrl & LShift Up::
Send {RCtrl Up}
MouseClick, left,,, 1, 0, U ; Release the mouse button.
return
While pressing ^LShift does simulate pressing RCtrl & Left click, releasing ^LShift does nothing most of the time. Usually, even if I release them, RCtrl + Left click keep being "pressed" and I have to manually press them to (activate and) deactivate them.

Try this
LCtrl & LShift::
Send {RCtrl Down}
MouseClick, left,,, 1, 0, D ; Hold down the left mouse button.
KeyWait, LCtrl ; Wait for LCtrl to be released
Send {RCtrl Up}
MouseClick, left,,, 1, 0, U ; Release the mouse button.
return
EDIT:
To make it work no matter the order in which you press the keys, try this:
LCtrl & LShift::
LShift & LCtrl::
Send {Blind}{Shift Up}
Send {RCtrl Down}
MouseClick, left,,, 1, 0, D ; Hold down the left mouse button.
KeyWait, LCtrl ; Wait for LCtrl to be released
Send {RCtrl Up}
MouseClick, left,,, 1, 0, U ; Release the mouse button.
return

Related

How to create context-sensitive hotkeys when no window is active?

I want to create some hotkeys when no window is active:
But there's no statement like #IfNoWinActive, how to achieve this?
There is always an active window (the one that will get input).
E.g. the Win+X Menu has no title but a hidden ahk_class (LauncherTipWnd). To detect it you have to add DetectHiddenWindows, On in your script:
#NoEnv
#SingleInstance Force
DetectHiddenWindows, On
#IfWinActive ahk_class LauncherTipWnd ; Win+X Menu
F1:: Run notepad
F2:: Send m ; starts the Device Manager
#IfWinActive
EDIT:
To get the title and ahk_class of the (hidden) active window, run this script and press F1 as soon as that window is active:
#NoEnv
#SingleInstance Force
DetectHiddenWindows, On
F1::
WinGetTitle, ActiveTitle, A
WinGetClass, ActiveClass, A
MsgBox, ActiveTitle: %ActiveTitle%`nActiveClass: ahk_class %ActiveClass%
return
F2::
WinSetTitle, A, , NewTitle
WinGetTitle, ActiveTitle, A
MsgBox, ActiveTitle: %ActiveTitle%
return
F3::
WinGet, ActiveExe, ProcessName, A
MsgBox, ProcessName: "%ActiveExe%"
return
Press Ctrl+C to copy the content of the MsgBox.
EDIT2:
In case the active window has no title and no ahk_class you can try this:
F1::
WinGetTitle, ActiveTitle, A
WinGetClass, ActiveClass, A
If (ActiveTitle = "" && ActiveClass = "")
Run notepad
; else If WinActive("WinTitle ahk_class WinClass", "WinText", "ExcludeTitlePart")
; do this
else
Send {F1}
Return
~Esc::
WinGet A, PID, A
if !A {
MsgBox ; 👈 put logic here
}
return

Is there a way to make multiple windows "active" at one time? in Windows?

Let's say a program that I created, an on-screen keyboard. I want to make it active so that it can receive my keyboard inputs, even while another program such as Google Chrome, or Notepad.exe is receiving my keyboard inputs as well. I mainly want my keyboard to run like "Nohboard" I am having trouble because once I get my keyboard looking and working like I want it, whenever I open another window, my keyboard will not work as long as "Windows 7" is focused on the "notepad.exe" for example. The reason I am making this is to monitor my keystrokes on-screen using my own software. I am trying to design my keyboard with gm studio 1.4 but without having the ability to make multiple windows active at once, my keyboard becomes useless everytime I click away from the keyboard program that I can create.
Is there any workarounds that can be used? Maybe a 3rd party script created by another program to allow my program's window to be "active" and receive keyboard keystroke inputs while another program is also active and receiving keyboard keystroke inputs?
Thanks in advance for anyone who can think of a solution that would be at all helpful.
Space::
SetTitleMatchMode, 2
ControlSend, , {Space Down}, Typing Lessons
ControlSend, , {Space Down}, kb
Keywait, Space
ControlSend, , {Space Up}, kb
ControlSend, , {Space Up}, Typing Lessons
Return
One thing that I've just figured out how to do, with autohotkey scripts is send keys to multiple programs. Is there a way to send it to the "last active window" and the "current active window" using autohotkey?
On a Windows System it is not possible to activate Multiple Windows at the same Time.
Only One Window can be Active. (Focused)
But there are two Ways that can help to Solved your Problem.
1 - You can for example your Onscreen Keyboard Window, to let ignore/disable Focus that Keyboard Window. (No Focus) (No activate + +E0x08000000)
Try This Ahk Script.
Note: This is a onscreen Keys Strip from F1 to F12
Example1.ahk
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption +Border +E0x08000000
Gui, Font, s07, Arial
Loop, 12
Gui, Add, Button, % "x" 5+(A_Index-1)*60 " y5 w60 h25 vF" A_Index " gButton", F%A_Index%
Gui, Show, y0 w730 h35 NoActivate
Return
Button:
SendInput, % "{" A_GuiControl "}"
Return
GuiClose:
ExitApp
Return
~esc::exitapp
2 - Or You can Use a 3rd party script,
note : run these 2 Scripts together. (KeypressValueToREG + ShowKeypressValue)
The First one is a Large Script but you can do many things, It Will Put All your Mouse and Keyboard Movements into Only One Windows Registry Key.
The Power of this KeypressValueToREG script is, you can run it in the background, and then you can use it in other Applications or other Languages Visual Studio/Visual Basic/c++/Python/Autohotkey/? They only need to Acces/Read that One Registry Key.
KeypressValueToREG.ahk
;KeypressValueToREG.ahk comes from KeypressOSD.ahk that was Created by Author RaptorX
; Open this Script in Wordpad and For Changelog look to the Bottom of the script.
;This code works with a getkeyname from a Dllcall (See Bottom Script- by Lexikos)
;you can press the esc key to exit.
#SingleInstance force
#NoEnv
SetBatchLines, -1
ListLines, Off
; Settings
global TransN := 200 ; 0~255
global ShowSingleKey := True
global ShowMouseButton := True
global ShowSingleModifierKey := True
global ShowModifierKeyCount := true
global ShowStickyModKeyCount := false
global DisplayTime := 2000 ; In milliseconds
global GuiPosition := "Bottom" ; Top or Bottom
global FontSize := 50
global GuiHeight := 115
CreateGUI()
CreateHotkey()
return
OnKeyPressed:
try {
key := GetKeyStr()
ShowHotkey(key)
SetTimer, HideGUI, % -1 * DisplayTime
}
return
OnKeyUp:
return
_OnKeyUp:
tickcount_start := A_TickCount
return
CreateGUI() {
global
Gui, +AlwaysOnTop -Caption +Owner +LastFound +E0x20
Gui, Margin, 0, 0
Gui, Color, Black
Gui, Font, cWhite s%FontSize% bold, Arial
Gui, Add, Text, vHotkeyText Center y20
WinSet, Transparent, %TransN%
}
CreateHotkey() {
Loop, 95
{
k := Chr(A_Index + 31)
k := (k = " ") ? "Space" : k
Hotkey, % "~*" k, OnKeyPressed
Hotkey, % "~*" k " Up", _OnKeyUp
}
Loop, 24 ; F1-F24
{
Hotkey, % "~*F" A_Index, OnKeyPressed
Hotkey, % "~*F" A_Index " Up", _OnKeyUp
}
Loop, 10 ; Numpad0 - Numpad9
{
Hotkey, % "~*Numpad" A_Index - 1, OnKeyPressed
Hotkey, % "~*Numpad" A_Index - 1 " Up", _OnKeyUp
}
Otherkeys := "WheelDown|WheelUp|WheelLeft|WheelRight|XButton1|XButton2|Browser_Forward|Browser_Back|Browser_Refresh|Browser_Stop|Browser_Search|Browser_Favorites|Browser_Home|Volume_Mute|Volume_Down|Volume_Up|Media_Next|Media_Prev|Media_Stop|Media_Play_Pause|Launch_Mail|Launch_Media|Launch_App1|Launch_App2|Help|Sleep|PrintScreen|CtrlBreak|Break|AppsKey|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|Tab|Enter|Esc|BackSpace"
. "|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause|sc145|sc146|sc046|sc123"
Loop, parse, Otherkeys, |
{
Hotkey, % "~*" A_LoopField, OnKeyPressed
Hotkey, % "~*" A_LoopField " Up", _OnKeyUp
}
If ShowMouseButton {
Loop, Parse, % "LButton|MButton|RButton", |
Hotkey, % "~*" A_LoopField, OnKeyPressed
}
for i, mod in ["Ctrl", "Shift", "Alt"] {
Hotkey, % "~*" mod, OnKeyPressed
Hotkey, % "~*" mod " Up", OnKeyUp
}
for i, mod in ["LWin", "RWin"]
Hotkey, % "~*" mod, OnKeyPressed
}
ShowHotkey(HotkeyStr) {
WinGetPos, ActWin_X, ActWin_Y, ActWin_W, ActWin_H, A
if !ActWin_W
throw
text_w := (ActWin_W > A_ScreenWidth) ? A_ScreenWidth : ActWin_W
;remove this gui codeline if you want only to Write the Value to Windows registry
;GuiControl, , HotkeyText, %HotkeyStr%
;GuiControl, , HotkeyText, %HotkeyStr%
RegWrite, REG_SZ, HKEY_CURRENT_USER,software\GetKeypressValue,KeypressValue,%HotkeyStr%
;remove this gui codeline if you want only to Write the Value to Windows registry
;GuiControl, Move, HotkeyText, w%text_w% Center
;GuiControl, Move, HotkeyText, w%text_w% Center
if (GuiPosition = "Top")
gui_y := ActWin_Y
else
gui_y := (ActWin_Y+ActWin_H) - 115 - 50
;remove this gui codeline if you want only to Write the Value to Windows registry
;Gui, Show, NoActivate x%ActWin_X% y%gui_y% h%GuiHeight% w%text_w%
;Gui, Show, NoActivate x%ActWin_X% y%gui_y% h%GuiHeight% w%text_w%
}
GetKeyStr() {
static modifiers := ["Ctrl", "Shift", "Alt", "LWin", "RWin"]
static repeatCount := 1
for i, mod in modifiers {
if GetKeyState(mod)
prefix .= mod " + "
}
if (!prefix && !ShowSingleKey)
throw
key := SubStr(A_ThisHotkey, 3)
if (key ~= "i)^(Ctrl|Shift|Alt|LWin|RWin)$") {
if !ShowSingleModifierKey {
throw
}
key := ""
prefix := RTrim(prefix, "+ ")
if ShowModifierKeyCount {
if !InStr(prefix, "+") && IsDoubleClickEx() {
if (A_ThisHotKey != A_PriorHotKey) || ShowStickyModKeyCount {
if (++repeatCount > 1) {
prefix .= " ( * " repeatCount " )"
}
} else {
repeatCount := 0
}
} else {
repeatCount := 1
}
}
} else {
if ( StrLen(key) = 1 ) {
key := GetKeyChar(key, "A")
} else if ( SubStr(key, 1, 2) = "sc" ) {
key := SpecialSC(key)
} else if (key = "LButton") && IsDoubleClick() {
key := "Double-Click"
}
_key := (key = "Double-Click") ? "LButton" : key
static pre_prefix, pre_key, keyCount := 1
global tickcount_start
if (prefix && pre_prefix) && (A_TickCount-tickcount_start < 300) {
if (prefix != pre_prefix) {
result := pre_prefix pre_key ", " prefix key
} else {
keyCount := (key=pre_key) ? (keyCount+1) : 1
key := (keyCount>2) ? (key " (" keyCount ")") : (pre_key ", " key)
}
} else {
keyCount := 1
}
pre_prefix := prefix
pre_key := _key
repeatCount := 1
}
return result ? result : prefix . key
}
SpecialSC(sc) {
static k := {sc046: "ScrollLock", sc145: "NumLock", sc146: "Pause", sc123: "Genius LuxeMate Scroll"}
return k[sc]
}
; by Lexikos - https://autohotkey.com/board/topic/110808-getkeyname-for-other-languages/#entry682236
GetKeyChar(Key, WinTitle:=0) {
thread := WinTitle=0 ? 0
: DllCall("GetWindowThreadProcessId", "ptr", WinExist(WinTitle), "ptr", 0)
hkl := DllCall("GetKeyboardLayout", "uint", thread, "ptr")
vk := GetKeyVK(Key), sc := GetKeySC(Key)
VarSetCapacity(state, 256, 0)
VarSetCapacity(char, 4, 0)
n := DllCall("ToUnicodeEx", "uint", vk, "uint", sc
, "ptr", &state, "ptr", &char, "int", 2, "uint", 0, "ptr", hkl)
return StrGet(&char, n, "utf-16")
}
IsDoubleClick(MSec = 300) {
Return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey < MSec)
}
IsDoubleClickEx(MSec = 300) {
preHotkey := RegExReplace(A_PriorHotkey, "i) Up$")
Return (A_ThisHotKey = preHotkey) && (A_TimeSincePriorHotkey < MSec)
}
HideGUI() {
Gui, Hide
}
~esc::exitapp
;---------------------------------------------
; ChangeLog : v2.22 (2017-02-25) - Now pressing the same combination keys continuously more than 2 times,
; for example press Ctrl+V 3 times, will displayed as "Ctrl + v (3)"
; v2.21 (2017-02-24) - Fixed LWin/RWin not poping up start menu
; v2.20 (2017-02-24) - Added displaying continuous-pressed combination keys.
; e.g.: With CTRL key held down, pressing K and U continuously will shown as "Ctrl + k, u"
; v2.10 (2017-01-22) - Added ShowStickyModKeyCount option
; v2.09 (2017-01-22) - Added ShowModifierKeyCount option
; v2.08 (2017-01-19) - Fixed a bug
; v2.07 (2017-01-19) - Added ShowSingleModifierKey option (default is True)
; v2.06 (2016-11-23) - Added more keys. Thanks to SashaChernykh.
; v2.05 (2016-10-01) - Fixed not detecting "Ctrl + ScrollLock/NumLock/Pause". Thanks to lexikos.
; v2.04 (2016-10-01) - Added NumpadDot and AppsKey
; v2.03 (2016-09-17) - Added displaying "Double-Click" of the left mouse button.
; v2.02 (2016-09-16) - Added displaying mouse button, and 3 settings (ShowMouseButton, FontSize, GuiHeight)
; v2.01 (2016-09-11) - Display non english keyboard layout characters when combine with modifer keys.
; v2.00 (2016-09-01) - Removed the "Fade out" effect because of its buggy.
; - Added support for non english keyboard layout.
; - Added GuiPosition setting.
; v1.00 (2013-10-11) - First release.
;--------------------------------------------
And the Second One it will Show Visual On your Desktop Screen All your Mouse and Keyboard Movements, You can Change this Script a little bit and then make your own keyboard that can show Visual all your keyboard Movements,
and if you use the trick from Example1.ahk then you can make a Professional Onscreen Keyboard Without focus the Window.
ShowKeypressValue.ahk
#SingleInstance force
Gui, +AlwaysOnTop -MaximizeBox ; -Caption +Resize -MinimizeBox +Disabled -SysMenu -Owner +OwnDialogs
Gui, Add, Text, center y10 h50 w300 vVar, %KeypressValue%
Gui, Color, White
Gui, show
size=20
Gui, Font, s%size%
GuiControl, Font, var
;run KeypressValueToREG.ahk - together with ShowKeypressValue.ahk
;The Features Are:
; - It will Show On your Screen, [All your Mouse Movements] and [All Keyboard Shortcuts Movement]
; - You can Make Scripts, that can do actions with MultiClicks on All Keyboard Shortcuts Clicks, How Cool Is that.
loop
{
RegRead, KeypressValue, HKEY_CURRENT_USER,software\GetKeypressValue,KeypressValue ; read KeypressValue
sleep 50
GuiControl,, var, %KeypressValue%
if (KeypressValue="Alt ( * 2 )") ;use this for [1x=Alt][2x=Alt ( * 2 )][3x=Alt ( * 3 )] [and many more]
{
;Here you can put any AHK CODE
msgbox you did click Alt 2x Times
}
if (KeypressValue="Alt ( * 3 )") ;use this for [1x=Alt][2x=Alt ( * 2 )][3x=Alt ( * 3 )] [and many more]
{
;Here you can put any AHK CODE
msgbox you did click Alt 3x Times
}
} ;End Loop
~esc::exitapp
Try this .
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
#InstallKeybdHook
#UseHook
; Add the keys you want send to the last active window in this array:
Keys := ["a","b","c","1","2","3","space",",",".","-","_","F3"] ; ...
for each, key in Keys
Hotkey, %key%, send_key_to_last_active_window, on
SetTimer get_last_active_window, 300 ; you can decrease this period (number of milliseconds that pass before the timer is executed)
return
send_key_to_last_active_window:
Send, {%A_ThisHotkey%}
; WinGetTitle, Title, ahk_id %LastActive_ID%
; WinGetClass, Class, ahk_id %LastActive_ID%
; ToolTip, last_active_window=%Title% ahk_class %Class%,,, 2
; If Class in Notepad,WordPadClass ; if the last active window is Notepad or WordPad
ControlSend,, {%A_ThisHotkey%}, ahk_id %LastActive_ID%
return
get_last_active_window:
WinGet, CurrentActive_ID, ID, A
If (CurrentActive_ID != NewActive_ID)
{
LastActive_ID = %NewActive_ID%
NewActive_ID := CurrentActive_ID
; WinGetTitle, NewActiveTitle, ahk_id %NewActive_ID%
; WinGetTitle, LastActiveTitle, ahk_id %LastActive_ID%
SetTimer get_last_active_window, off
; CoordMode, ToolTip, Screen
; ToolTip, Current active window:`nTitle: %NewActiveTitle%`nID: %NewActive_ID%`n`nLast active window:`nTitle: %LastActiveTitle%`nID: %LastActive_ID%, 0, 0, 1
WinWaitNotActive, ahk_id %NewActive_ID%
SetTimer get_last_active_window, on
}
return
See For-Loop, Hotkey and SetTimer in the documentation.

Can I edit the context menu of a text field (not Explorer context menu)?

I want to add an entry to the context menu of a text field, eg:
I have an AutoHotkey executable that will put highlighted text into the clipboard, then execute a Google search in Chrome with the clipboard. I would like a registry addition that gives me a single-click in the menu, that runs that executable. I have achieved this in the Explorer context menu (for processing of files with third-party applications, etc), but I have found no mention anywhere of the ability to change this specific menu.
The question asks how to edit the context menu for an Edit control,
and provides an image showing an Edit control context menu, present
while renaming a file in Explorer.
One approach would be to show a custom context menu,
when an Edit control is right-clicked,
or when an Edit control is focused and the AppsKey is pressed.
The AutoHotkey script below provides such functionality
for Explorer (tested on Windows 7), it replicates
the Edit control menu and adds a button that opens the selected
text in the default web browser.
Note: The Explorer address bar also uses an Edit control,
however, this is taken into account by the script.
;AutoHotkey script for:
;windows - Can I edit the context menu of a text field (not Explorer context menu)? - Stack Overflow
;http://stackoverflow.com/questions/39827324/can-i-edit-the-context-menu-of-a-text-field-not-explorer-context-menu/41343741#41343741
;see also:
;contextmenu - Can I add a custom paste option to the windows text editing context menu? - Stack Overflow
;http://stackoverflow.com/questions/17370415/can-i-add-a-custom-paste-option-to-the-windows-text-editing-context-menu/41343891#41343891
;tested on Windows 7
GroupAdd, WinGroupFolder, ahk_class CabinetWClass ;explorer
#IfWinActive, ahk_group WinGroupFolder
$RButton Up:: ;explorer - custom Edit control menu
$AppsKey:: ;explorer - custom Edit control menu
;#IfWinActive, ahk_class Notepad
;$RButton Up:: ;notepad - custom Edit control menu
;$AppsKey:: ;notepad - custom Edit control menu
;STAGE - create menu if not already created
if !vIsReady
{
Menu, EditMenu, Add, &My Item, MyItem
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, &Undo, EditUndo
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Cu&t, EditCut
Menu, EditMenu, Add, &Copy, EditCopy
Menu, EditMenu, Add, &Paste, EditPaste
Menu, EditMenu, Add, &Delete, EditDelete
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Select &All, EditSelectAll
VarSetCapacity(vPos1, 4), VarSetCapacity(vPos2, 4)
VarSetCapacity(vPos1X, 4), VarSetCapacity(vPos2X, 4)
vIsReady := 1
}
;STAGE - perform certain checks, if any of them fail
;then let hotkeys perform their normal function,
;start by stating that, so far, the checks have not failed
vRet := 1
;check - if active control is an Edit/RichEdit control
if vRet
{
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
ControlGet, hCtl, Hwnd, , %vCtlClassNN%, ahk_id %hWnd%
WinGetClass, vWinClass, ahk_id %hCtl%
if !(SubStr(vWinClass, 1, 4) = "Edit") && !(SubStr(vWinClass, 1, 8) = RichEdit)
vRet := 0
}
;check - if a right-click was performed, the control
;under the cursor must be the active control
if vRet && InStr(A_ThisHotkey, "RButton")
{
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, , hCtl2, 3
if !(hCtl2 = hCtl)
vRet := 0
}
;check - the Edit control must be for a file icon and not the address bar
if vRet
{
;hWndParent := DllCall("user32\GetParent", Ptr,hCtl, Ptr)
hWndParent := DllCall("user32\GetAncestor", Ptr,hCtl, UInt,1, Ptr) ;GA_PARENT := 1
WinGetClass, vWinClassParent, ahk_id %hWndParent%
if (vWinClassParent = "ComboBox")
vRet := 0
}
;if a check has failed, then let hotkeys perform their normal function
if !vRet
{
if InStr(A_ThisHotkey, "RButton")
SendInput {Click right}
if InStr(A_ThisHotkey, "AppsKey")
SendInput {AppsKey}
Return
}
;STAGE - if clicked Edit control, menu will appear
;relative to cursor coordinates retrieved earlier,
;if pressed AppsKey, menu will appear in centre of Edit control
if !InStr(A_ThisHotkey, "RButton")
{
WinGetPos, vPosX, vPosY, vPosW, vPosH, ahk_id %hCtl%
vPosX += vPosW/2, vPosY += vPosH/2
}
;STAGE - retrieve information from Edit control
;and disable menu items accordingly
;Undo - check undo status (is undo available)
;Cut - check text selection > 0
;Copy - check text selection > 0
;Paste - check clipboard not empty
;Delete - check text selection > 0
;Select All - always available
SendMessage, 0xC6, 0, 0, , ahk_id %hCtl% ;EM_CANUNDO := 0xC6
vOptU := ErrorLevel ? "En" : "Dis" ;1=undo available/0=undo not available
ControlGet, vText, Selected, , , ahk_id %hCtl%
vOptT := StrLen(vText) ? "En" : "Dis"
vOptC := StrLen(Clipboard) ? "En" : "Dis"
Menu, EditMenu, % vOptU "able", &Undo, EditUndo
Menu, EditMenu, % vOptT "able", Cu&t, EditCut
Menu, EditMenu, % vOptT "able", &Copy, EditCopy
Menu, EditMenu, % vOptC "able", &Paste, EditPaste
Menu, EditMenu, % vOptT "able", &Delete, EditDelete
;STAGE - get Edit control character positions
;(unfortunately showing the custom menu ends the rename mode,
;we get the Edit control character positions in order to restore them later)
SendMessage, 0xB0, &vPos1, &vPos2, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1 := NumGet(vPos1), vPos2 := NumGet(vPos2)
;STAGE - show menu
CoordMode, Menu, Screen
Menu, EditMenu, Show, %vPosX%, %vPosY%
Return
;==============================
;STAGE - replicate standard Edit control menu items
;(or perform custom menu function)
;(unfortunately showing the custom menu ends the rename mode,
;so the Edit control has to be put into rename again,
;and the character positions restored)
EditUndo:
EditCut:
EditCopy:
EditPaste:
EditDelete:
EditSelectAll:
MyItem:
;STAGE - enter rename mode again
IfWinActive, ahk_group WinGroupFolder
{
SendInput {F2}
Loop, 20
{
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
if (SubStr(vCtlClassNN, 1, 4) = "Edit")
break
Sleep 50
}
if !(SubStr(vCtlClassNN, 1, 4) = "Edit")
{
MsgBox % "error"
Return
}
ControlGet, hCtl, Hwnd, , % vCtlClassNN, ahk_id %hWnd%
;STAGE - restore character positions
if !InStr(A_ThisLabel, "SelectAll") && !InStr(A_ThisLabel, "MyItem")
{
vRet := 0
Loop, 100
{
SendMessage, 0xB1, vPos1, vPos2, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
SendMessage, 0xB0, &vPos1X, &vPos2X, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1X := NumGet(vPos1X), vPos2X := NumGet(vPos2X)
if (vPos1 = vPos1X) && (vPos2 = vPos2X)
{
vRet := 1
break
}
Sleep 50
if !vRet
{
MsgBox % "error"
Return
}
}
}
}
;STAGE - perform standard Edit control menu functions
if InStr(A_ThisLabel , "Undo")
SendMessage, 0x304, , , , ahk_id %hCtl% ;WM_UNDO := 0x304
if InStr(A_ThisLabel , "Cut")
SendMessage, 0x300, , , , ahk_id %hCtl% ;WM_CUT := 0x300
if InStr(A_ThisLabel , "Copy")
SendMessage, 0x301, , , , ahk_id %hCtl% ;WM_COPY := 0x301
if InStr(A_ThisLabel , "Paste")
SendMessage, 0x302, , , , ahk_id %hCtl% ;WM_PASTE := 0x302
if InStr(A_ThisLabel , "Delete")
SendMessage, 0x303, , , , ahk_id %hCtl% ;WM_CLEAR := 0x303
if InStr(A_ThisLabel , "SelectAll")
SendMessage, 0xB1, 0, -1, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
;STAGE - actions to take if user chooses custom menu item
if 0 ;this comments out the 6 lines below
if InStr(A_ThisLabel , "MyItem")
{
vText := "My String"
;ControlSend, , % vText, ahk_id %hCtl% ;use SendInput instead since capitalisation can be unreliable
SendInput {Raw}%vText%
}
;STAGE - actions to take if user chooses custom menu item
if InStr(A_ThisLabel , "MyItem") && !(vText = "")
{
MsgBox, 0x40003, , % "Choose 'Yes' to search for:`r`n" vText
IfMsgBox Yes
{
vUrl := "http://www.google.co.uk/search?q=" UriEncode(vText)
Run, "%vUrl%"
;Run, chrome.exe "%vUrl%"
}
}
Return
#IfWinActive
;==================================================
;URL encoding - Rosetta Code
;https://www.rosettacode.org/wiki/URL_encoding#AutoHotkey
; Modified from https://autohotkey.com/board/topic/75390-ahk-l-unicode-uri-encode-url-encode-function/?p=480216
UriEncode(Uri)
{
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
StrPut(Uri, &Var, "UTF-8")
f := A_FormatInteger
SetFormat, IntegerFast, H
While Code := NumGet(Var, A_Index - 1, "UChar")
If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
SetFormat, IntegerFast, %f%
Return, Res
}
;==================================================
I will describe editing a file's menu first, and then editing an Edit control's menu afterwards.
File's menu:
If you know how to safely use RegEdit,
creating this registry entry, will add the option 'My Script' to the right-click context menu for all files:
HKEY_CLASSES_ROOT\*\Shell\My Script\command
(Default) REG_SZ "C:\Program Files\AutoHotkey\AutoHotkey.exe" "C:\Program Files\AutoHotkey\My Script.ahk" "%1"
Clicking on the menu item will launch the script.
If you want to do something to the file that was clicked, it's path can be retrieved via:
vPath = %1%
To operate on more than one file at the same time requires a shell extension, which is more complicated.
Edit control's menu:
The context menu in your image is an Edit control's context menu.
To change that menu would be more difficult. When you rename a file, you are editing text on a small Edit control.
To 'add' a menu item, the simplest method would be to replace the entire menu with your own custom context menu. With your custom menu item on top, and you would probably want to recreate the Undo/Cut/Copy/Paste/Delete/Select All items that operate on the Edit control. Using ControlGet, vText, Selected to recreate the Copy function for example. You would also need to capture right-clicks via the RButton hotkey, and use ControlGetFocus to check if an Edit control was in focus, and MouseGetPos to check if an Edit control was under the cursor. So there would be some work involved. Regarding capturing right-clicks, see the link below, where you would replace LButton with RButton. Good luck!
Is it possible to catch the close button and minimize the window instead? AutoHotKey
;Menu Google Search Autohotkey script: select any text in any text edit field, then press
AppsKey & RButton::
^+g:: ; or press Ctrl+Shift+g
Menu,GoogleSearch,Add,Search with Google,GoogleSearch
Menu,GoogleSearch,Show
return
GoogleSearch:
{
Send, ^c
Sleep 50
Run, http://www.google.com/search?q=%clipboard%
Return
}

AutoHotKey LWin + Shift + Left/Right to Shift + Home/End

I'm trying to make a script for AutoHotKey to map the LWin + Shift + Left/Right key combination to Shift + Home/End.
I also want to make LWin + Left/Right do Home/End, but I got this working.
Here's what I am trying right now:
LWin & Right::
GetKeyState, state, Shift
if state = D
Send {Shift}{End}
Else Send {End}
Return
Sorry, just played around with the script for a bit and got it working:
LWin & Left::
GetKeyState, state, Shift
If state = D
Send +{Home}
Else Send {Home}
Return
LWin & Right::
GetKeyState, state, Shift
If state = D
Send +{End}
Else Send {End}
Return

Can I add a custom paste option to the windows text editing context menu?

I am looking for a way to add an option(s) to the right click context menu when editing text on a WinXP machine. I already do a lot of copy/pasting on it, so the clipboard is always changing, but there is one string I type repeatedly in almost every file I edit.
I've already added some custom option to the context menu for .zip files to batch unzip them, but I'm not having any luck finding a way to add this.
The machine is used for a single purpose and I try to keep it as stable as possible so I'm trying to stay away from any new third-party software that could bloat the system.
I don't think there's an extension point for that sort of thing. You'd have to inject code into every process that has a window with a text box control, which would be complicated and frowned upon by most anti-virus applications.
I know you said you wanted to avoid third-party software, but there really isn't any way around it. A program like AutoIt will allow you to create a custom keyboard shortcut to paste whatever text you like into almost any application. It would probably be much more stable than any custom program written in the short term.
You can even compile the automation script to a standalone executable if you don't want to install the entire AutoIt distribution on the machine.
Assuming you are referring to the Edit control context menu.
You can achieve this by cloning and amending the Edit control context menu, via AutoHotkey. If the context menu is a for a different type of control, the same principle applies but it may be harder to recreate the existing menu item functions.
To 'add' a menu item, the simplest method would be to replace the entire menu with your own custom context menu. With your custom menu item at the top of it, and you would probably want to recreate the Undo/Cut/Copy/Paste/Delete/Select All items that appear on the Edit control. Using ControlGet, vText, Selected to recreate the Copy function for example. You use #IfWinActive to make
the menus only appear if a certain window is the active window, e.g. only if Notepad is the active window.
You would also need to capture right-clicks via the RButton hotkey and/or capture AppsKey presses,
and use ControlGetFocus to check if an Edit control was in focus, and MouseGetPos to check if an Edit control was under the cursor. So there would be a bit of work involved. Regarding capturing right-clicks, see the link below, where you would replace LButton with RButton. Good luck!
Is it possible to catch the close button and minimize the window instead? AutoHotKey
Similar question:
Can I edit the context menu of a text field (not Explorer context menu)?
Note:
- For typing long/repetitive strings, the use of hotstrings in AutoHotkey can really facilitate this. Achievable in literally one line of code.
- For batch jobs involving zip files perhaps try 7-Zip and using command lines parameters in AutoHotkey. This could probably be achieved in around 10 or 20 lines of code.
AutoHotkey is very lightweight, about 1MB, you could try it for a day or two, possibly watch a short 'hello world' tutorial video, it can be quite easy to get started.
The question asks how to edit the context menu for an Edit control,
it it slightly unclear whether this is wanted for renaming or editing files,
the AutoHotkey script below replicates
the Edit control menu when editing files in Explorer and using Notepad.
It adds a button that sends a string to the Edit control.
The script shows a custom context menu,
when an Edit control is right-clicked,
or when an Edit control is focused and the AppsKey is pressed.
Note: The script below is tested on Windows 7,
but the methods should work on Windows XP.
Note: The Explorer address bar also uses an Edit control,
however, this is taken into account by the script.
Note: You requested a method that is lightweight,
AutoHotkey can be run with one exe file (under 2MB in size),
and one script file. Scripts can also be compiled to small exes.
;AutoHotkey script for:
;contextmenu - Can I add a custom paste option to the windows text editing context menu? - Stack Overflow
;http://stackoverflow.com/questions/17370415/can-i-add-a-custom-paste-option-to-the-windows-text-editing-context-menu/41343891#41343891
;see also:
;windows - Can I edit the context menu of a text field (not Explorer context menu)? - Stack Overflow
;http://stackoverflow.com/questions/39827324/can-i-edit-the-context-menu-of-a-text-field-not-explorer-context-menu/41343741#41343741
;tested on Windows 7
GroupAdd, WinGroupFolder, ahk_class CabinetWClass ;explorer
#IfWinActive, ahk_group WinGroupFolder
$RButton Up:: ;explorer - custom Edit control menu
$AppsKey:: ;explorer - custom Edit control menu
#IfWinActive, ahk_class Notepad
$RButton Up:: ;notepad - custom Edit control menu
$AppsKey:: ;notepad - custom Edit control menu
;STAGE - create menu if not already created
if !vIsReady
{
Menu, EditMenu, Add, &My Item, MyItem
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, &Undo, EditUndo
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Cu&t, EditCut
Menu, EditMenu, Add, &Copy, EditCopy
Menu, EditMenu, Add, &Paste, EditPaste
Menu, EditMenu, Add, &Delete, EditDelete
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Select &All, EditSelectAll
VarSetCapacity(vPos1, 4), VarSetCapacity(vPos2, 4)
VarSetCapacity(vPos1X, 4), VarSetCapacity(vPos2X, 4)
vIsReady := 1
}
;STAGE - perform certain checks, if any of them fail
;then let hotkeys perform their normal function,
;start by stating that, so far, the checks have not failed
vRet := 1
;check - if active control is an Edit/RichEdit control
if vRet
{
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
ControlGet, hCtl, Hwnd, , %vCtlClassNN%, ahk_id %hWnd%
WinGetClass, vWinClass, ahk_id %hCtl%
if !(SubStr(vWinClass, 1, 4) = "Edit") && !(SubStr(vWinClass, 1, 8) = RichEdit)
vRet := 0
}
;check - if a right-click was performed, the control
;under the cursor must be the active control
if vRet && InStr(A_ThisHotkey, "RButton")
{
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, , hCtl2, 3
if !(hCtl2 = hCtl)
vRet := 0
}
;check - the Edit control must be for a file icon and not the address bar
if vRet
{
;hWndParent := DllCall("user32\GetParent", Ptr,hCtl, Ptr)
hWndParent := DllCall("user32\GetAncestor", Ptr,hCtl, UInt,1, Ptr) ;GA_PARENT := 1
WinGetClass, vWinClassParent, ahk_id %hWndParent%
if (vWinClassParent = "ComboBox")
vRet := 0
}
;if a check has failed, then let hotkeys perform their normal function
if !vRet
{
if InStr(A_ThisHotkey, "RButton")
SendInput {Click right}
if InStr(A_ThisHotkey, "AppsKey")
SendInput {AppsKey}
Return
}
;STAGE - if clicked Edit control, menu will appear
;relative to cursor coordinates retrieved earlier,
;if pressed AppsKey, menu will appear in centre of Edit control
if !InStr(A_ThisHotkey, "RButton")
{
WinGetPos, vPosX, vPosY, vPosW, vPosH, ahk_id %hCtl%
vPosX += vPosW/2, vPosY += vPosH/2
}
;STAGE - retrieve information from Edit control
;and disable menu items accordingly
;Undo - check undo status (is undo available)
;Cut - check text selection > 0
;Copy - check text selection > 0
;Paste - check clipboard not empty
;Delete - check text selection > 0
;Select All - always available
SendMessage, 0xC6, 0, 0, , ahk_id %hCtl% ;EM_CANUNDO := 0xC6
vOptU := ErrorLevel ? "En" : "Dis" ;1=undo available/0=undo not available
ControlGet, vText, Selected, , , ahk_id %hCtl%
vOptT := StrLen(vText) ? "En" : "Dis"
vOptC := StrLen(Clipboard) ? "En" : "Dis"
Menu, EditMenu, % vOptU "able", &Undo, EditUndo
Menu, EditMenu, % vOptT "able", Cu&t, EditCut
Menu, EditMenu, % vOptT "able", &Copy, EditCopy
Menu, EditMenu, % vOptC "able", &Paste, EditPaste
Menu, EditMenu, % vOptT "able", &Delete, EditDelete
;STAGE - get Edit control character positions
;(unfortunately showing the custom menu ends the rename mode,
;we get the Edit control character positions in order to restore them later)
SendMessage, 0xB0, &vPos1, &vPos2, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1 := NumGet(vPos1), vPos2 := NumGet(vPos2)
;STAGE - show menu
CoordMode, Menu, Screen
Menu, EditMenu, Show, %vPosX%, %vPosY%
Return
;==============================
;STAGE - replicate standard Edit control menu items
;(or perform custom menu function)
;(unfortunately showing the custom menu ends the rename mode,
;so the Edit control has to be put into rename again,
;and the character positions restored)
EditUndo:
EditCut:
EditCopy:
EditPaste:
EditDelete:
EditSelectAll:
MyItem:
;STAGE - enter rename mode again
IfWinActive, ahk_group WinGroupFolder
{
SendInput {F2}
Loop, 20
{
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
if (SubStr(vCtlClassNN, 1, 4) = "Edit")
break
Sleep 50
}
if !(SubStr(vCtlClassNN, 1, 4) = "Edit")
{
MsgBox % "error"
Return
}
ControlGet, hCtl, Hwnd, , % vCtlClassNN, ahk_id %hWnd%
;STAGE - restore character positions
if !InStr(A_ThisLabel, "SelectAll")
{
vRet := 0
Loop, 100
{
SendMessage, 0xB1, vPos1, vPos2, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
SendMessage, 0xB0, &vPos1X, &vPos2X, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1X := NumGet(vPos1X), vPos2X := NumGet(vPos2X)
if (vPos1 = vPos1X) && (vPos2 = vPos2X)
{
vRet := 1
break
}
Sleep 50
if !vRet
{
MsgBox % "error"
Return
}
}
}
}
;STAGE - perform standard Edit control menu functions
if InStr(A_ThisLabel , "Undo")
SendMessage, 0x304, , , , ahk_id %hCtl% ;WM_UNDO := 0x304
if InStr(A_ThisLabel , "Cut")
SendMessage, 0x300, , , , ahk_id %hCtl% ;WM_CUT := 0x300
if InStr(A_ThisLabel , "Copy")
SendMessage, 0x301, , , , ahk_id %hCtl% ;WM_COPY := 0x301
if InStr(A_ThisLabel , "Paste")
SendMessage, 0x302, , , , ahk_id %hCtl% ;WM_PASTE := 0x302
if InStr(A_ThisLabel , "Delete")
SendMessage, 0x303, , , , ahk_id %hCtl% ;WM_CLEAR := 0x303
if InStr(A_ThisLabel , "SelectAll")
SendMessage, 0xB1, 0, -1, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
;STAGE - actions to take if user chooses custom menu item
if InStr(A_ThisLabel , "MyItem")
{
vText := "My String"
;ControlSend, , % vText, ahk_id %hCtl% ;use SendInput instead since capitalisation can be unreliable
SendInput {Raw}%vText%
}
;STAGE - actions to take if user chooses custom menu item
if 0 ;this comments out the 9 lines below
if InStr(A_ThisLabel , "MyItem") && !(vText = "")
{
MsgBox, 0x40003, , % "Choose 'Yes' to search for:`r`n" vText
IfMsgBox Yes
{
vUrl := "http://www.google.co.uk/search?q=" UriEncode(vText)
Run, "%vUrl%"
}
}
Return
#IfWinActive
;==================================================
;URL encoding - Rosetta Code
;https://www.rosettacode.org/wiki/URL_encoding#AutoHotkey
; Modified from https://autohotkey.com/board/topic/75390-ahk-l-unicode-uri-encode-url-encode-function/?p=480216
UriEncode(Uri)
{
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
StrPut(Uri, &Var, "UTF-8")
f := A_FormatInteger
SetFormat, IntegerFast, H
While Code := NumGet(Var, A_Index - 1, "UChar")
If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
SetFormat, IntegerFast, %f%
Return, Res
}
;==================================================

Resources