Arrow keys do not work in context menu at first time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Arrow keys do not work in context menu at first time

22 Apr 2016, 06:00

Code: Select all

Menu, MyMenu, Add, Desktop, 1
Menu, MyMenu, Add, Programs, 2
Return

F1:: Menu, MyMenu, Show

1:
Run, %A_Desktop%
Return

2:
Run, %A_ProgramFiles%
Return
If I run this code, press f1, and press "up" or "down" key - nothing happens.
But if I close this menu and press once again f1 - "up" and "down" keys work.
Or I can run this script, then reload it, then press "f1" - "up" and "down" keys work.
Why "up" and "down" keys do not work at first time?
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 06:54

You might need to activate the GUI first.

Code: Select all

Menu, MyMenu, Add, Desktop, 1
Menu, MyMenu, Add, Programs, 2

F1::
Menu, MyMenu, Show
Gui +LastFound
WinActivate
return
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 07:56

Oh duh, that's not a GUI so my suggestion is wrong. I've confirmed that your intended behavior works with other files, but not AHK scripts, so there's some command missing but I don't know what it is.
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 08:02

I cannot confirm this behavior. It worked properly for me when I put it into my test script. My auto-execute section, in case it makes any difference, is this:

Code: Select all

#If
SetWorkingDir, %A_ScriptDir%
#SingleInstance, Force
SetTitleMatchMode, 2
SetKeyDelay, 70
SetWinDelay, -1
CoordMode, ToolTip
CoordMode, Mouse, Screen
I am running AHK 32-bit Unicode v1.1.23.01 on Windows 8.1.

A suggestion for you may be doing this:

Code: Select all

F1::
Menu, MyMenu, Show
Menu, MyMenu, Hide
Menu, MyMenu, Show
return
However, if there is a perceptible flicker everytime you do this, you might work around it with a variable:

Code: Select all

F1::
If !recurrent
{
Menu, MyMenu, Show
Menu, MyMenu, Hide
recurrent:=true
}
Menu, MyMenu, Show
return
So only one Show is executed on the subsequent F1 presses.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 08:52

I am running AHK 32-bit Unicode v1.1.23.03 on Win7.
I can not find this command:

Code: Select all

Menu, MyMenu, Hide
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 09:23

It worked fine for me as-is, first time. Windows 7 64-bit, AHK 1.1.23.05 Unicode 64-bit
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 09:45

I have found that there are times the pop-up menu does not automatically get focus when it is opened. I work around this by doing something like the following...

Code: Select all

Menu, MyMenu, Add, Item1, MenuHandler
Menu, MyMenu, Add, Item2, MenuHandler
SetTimer, ActivateMe, -25
Menu, MyMenu, Show
Return

MenuHandler:
MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%.
return

ActivateMe:
WinActivate, %A_ScriptName%
Return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 10:05

Ooops, there is no Hide command apparently. And Shadow reminded me that Menu will freeze a thread until something is selected (I think). My idea would only messily work with a second script. Shadow, does your idea work? My understanding is that the Timer won't fire until the menu has been dismissed.
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 10:25

The code I posted works for me using AHK 1.1.23.05 Unicode 64-bit on Windows 7. I have no idea why it works, because the Menu, MyMenu, Show does freeze the thread (which can be confirmed by putting additional commands into the timer).
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 11:57

Arrow keys do not work for me with this code:

Code: Select all

Menu, MyMenu, Add, Item1, MenuHandler
Menu, MyMenu, Add, Item2, MenuHandler
SetTimer, ActivateMe, -25
Menu, MyMenu, Show
Return
 
MenuHandler:
MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%.
return
 
ActivateMe:
WinActivate, %A_ScriptName%
Return
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 12:50

But this code works.
I think this is bug.

Code: Select all

DetectHiddenWindows, on
WinActivate, ahk_class AutoHotkey
Menu, MyMenu, Add, Desktop, 1
Menu, MyMenu, Add, Programs, 2
Return
 
F1:: Menu, MyMenu, Show
 
1:
Run, %A_Desktop%
Return
 
2:
Run, %A_ProgramFiles%
Return
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Arrow keys do not work in context menu at first time

22 Apr 2016, 21:10

Here I found some information:
Using shortkeys with TrackPopupMenu
If you are using shortkeys in your popup menu e.g. &About, E&xit then make sure SetForegroundWindow() is called before TrackPopupMenu().
Otherwise when the popup menu is activated pressing the shortkeys (A or x) will have no impact / not work.
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

I have written bug report:
https://autohotkey.com/boards/viewtopic ... 14&t=16809

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 124 guests