Menu command, active window loses focus

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Menu command, active window loses focus

23 Jan 2017, 00:33

I am trying to clone and add an item to the Edit control right-click menu (e.g. Notepad).
But the active window temporarily loses focus.
It appears this issue is a known one:

Menu without causing loss of focus? - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7201 ... -of-focus/

Menu focus problem - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/9071 ... s-problem/

Have we got any further with this?

Code: Select all

q::
if !vIsReady
{
	CoordMode, Mouse, Screen
	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
	vIsReady := 1
}

WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
WinGetClass, vWinClass, % "ahk_id " hCtl
if !(vWinClass = "Edit")
	return

MouseGetPos, vPosX, vPosY

;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

Menu, EditMenu, Show, % vPosX, % vPosY
return

;==============================

MyItem:
MsgBox, % "hello world!"
return

;==============================

EditUndo:
SendMessage, 0x304,,,, % "ahk_id " hCtl ;WM_UNDO := 0x304
return

EditCut:
SendMessage, 0x300,,,, % "ahk_id " hCtl ;WM_CUT := 0x300
return

EditCopy:
SendMessage, 0x301,,,, % "ahk_id " hCtl ;WM_COPY := 0x301
return

EditPaste:
SendMessage, 0x302,,,, % "ahk_id " hCtl ;WM_PASTE := 0x302
return

EditDelete:
SendMessage, 0x303,,,, % "ahk_id " hCtl ;WM_CLEAR := 0x303
return

EditSelectAll:
SendMessage, 0xB1, 0, -1,, % "ahk_id " hCtl ;EM_SETSEL := 0xB1
return

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: bapl, Bing [Bot], dentopolis, gidean1, Green Astronaut, kghaleh, murataygun, sanmaodo, XMCQCX and 66 guests