[SOLVED] Blocking the keyboard Windows key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

[SOLVED] Blocking the keyboard Windows key

21 Mar 2018, 17:34

I never use the "Windows" keyboard key for what Microsoft intended it for. Only use it in combination with another key to activate an Autohotkey macro. Is it possible to block the keyboard "Windows" key from activating the menu but function when it's used with another key? I ask because just touching this key, as I am about to activate a macro, the Windows menu comes up. I am using Windows 7 Ultimate, 32bit.
Last edited by ineuw on 10 Feb 2023, 11:35, edited 1 time in total.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Blocking the keyboard Windows key

22 Mar 2018, 03:14

From AHK's help file ...
Modifier State: When Send is required to change the state of the Win or Alt modifier keys (such as if the user was holding one of those keys), it may inject additional keystrokes (Ctrl by default) to prevent the Start menu or window menu from appearing. For details, see #MenuMaskKey.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Blocking the keyboard Windows key

22 Mar 2018, 05:04

- By testing pressing the Win key, it seems that as you hold it down, nothing happens, but that the moment you release it, the menu appears.
- Thus, in theory, an 'LWin up' hotkey could help to prevent the menu from appearing, but otherwise retain normal functionality.
- However the script below isn't quite working, for Win+M, the MsgBox is not showing, although it is showing for Ctrl+Win+M. If anyone can help to explain that. Thanks.

Code: Select all

#m::
MsgBox, % A_ThisHotkey
return

^#m::
MsgBox, % A_ThisHotkey
return

LWin up::
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Blocking the keyboard Windows key

22 Mar 2018, 05:40

Hallo,
try:

Code: Select all

~LWin::vk07

#m::
MsgBox, % A_ThisHotkey
return

^#m::
MsgBox, % A_ThisHotkey
return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Blocking the keyboard Windows key

22 Mar 2018, 05:48

- Many thanks Rohwedder, your script appears to work. Very interesting, I wonder how you came across the idea.
- According to this link, vk07 is an undefined key.
Virtual-Key Codes (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
- So you're remapping the LWin key by itself to an undefined key, although, I suppose LWin when used as a modifier is still working as usual.
- Btw I would still be interested if anyone can explain why my original script didn't work with Win+M.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Blocking the keyboard Windows key

22 Mar 2018, 08:21

According to this link, vk07 is an undefined key.
And according to this link, you're damn right: https://autohotkey.com/docs/commands/_MenuMaskKey.htm :mrgreen:
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Blocking the keyboard Windows key

22 Mar 2018, 08:30

jeeswg wrote:- Many thanks Rohwedder, your script appears to work. Very interesting, I wonder how you came across the idea.
- According to this link, vk07 is an undefined key.
Virtual-Key Codes (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
- So you're remapping the LWin key by itself to an undefined key, although, I suppose LWin when used as a modifier is still working as usual.
- Btw I would still be interested if anyone can explain why my original script didn't work with Win+M.
Maybe it has something to do with the fact that win+m is a windows hotkey? Just a vague probability. Because I know that in order to remap window hotkeys you need to do something with the registry.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Blocking the keyboard Windows key

22 Mar 2018, 09:01

Failing that, you could use AutoHotInterception to block that key at a driver level, without having to assign it to a hotkey.

Code: Select all

#include Lib\AutoHotInterception.ahk
global Interception := AutoHotInterception_Init()

VID := 0x04F2, PID := 0x0112 ; Set this to the VID and PID of your keyboard
Interception.SubscribeKey(GetKeySC("LWin"), true, Func("KeyEvent"), VID, PID)
return

KeyEvent(state){
	ToolTip % "State: " state
}
Edit: Ah, I see that the ScanCode for LWin is >256, making it an extended key, which AHI does not currently support.
I know how to fix it, I can take a look at pushing out a new version of AHI tonight when I get home.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Blocking the keyboard Windows key

22 Mar 2018, 09:34

- I had been interested in replacing the Win menu, but maintaining normal Win key functionality everywhere else. This doesn't quite work, the replacement menu pops up when you don't want it to. [EDIT:] It appears to work pretty well since I added the RegExMatch line. It checks for the presence of '#' with at least one character after it, to avoid false positives with hotkeys that use the # key.

Code: Select all

~LWin::vk07

#m::
MsgBox, % A_ThisHotkey
return

^#m::
MsgBox, % A_ThisHotkey
return

LWin up::
if RegExMatch(A_PriorHotkey, "#(?=.)")
	return
InputBox, vInput,, menu
return
@Bobo: Thanks for the link.
@Nwb: Not in this particular case, cheers though. (Btw I use Windows 7.)
@evilC: Have you come across any workarounds for capturing Fn, or Fn+other keys? Also, have you come across any manufacturers where the Fn key is AHK-friendly? If not I might start a thread about manufacturers and friendly Fn keys. Cheers.
Last edited by jeeswg on 22 Mar 2018, 09:40, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Blocking the keyboard Windows key

22 Mar 2018, 09:36

If the key has a Scan Code, AHI should be able to block it and fire a function when it changes state.
AHI uses the Interception keyboard driver. It sees EVERYTHING from the keyboard. If the driver does not see the key, then windows cannot possibly see the key ;)
Edit: I spose windows may not actually see the Fn key - it is possible I suppose that the key works at an electronics level, and windows is entirely unaware of it's existence.
So maybe not quite as sure-fire as I first thought.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Blocking the keyboard Windows key

22 Mar 2018, 17:33

Much thanks to all for the help. This works perfectly.

Code: Select all

~LWin::vk07
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Blocking the keyboard Windows key

24 Mar 2018, 00:30

jeeswg wrote:However the script below isn't quite working, for Win+M, the MsgBox is not showing, although it is showing for Ctrl+Win+M. If anyone can help to explain that.
If you block LWin up and not LWin down, you will be left with LWin stuck in the pressed state. AutoHotkey protects you from yourself in this case, by having LWin up:: block both press and release, as documented. Since it lacks the wildcard modifier, it does not activate (or block LWin) if you are pressing other modifiers, such as Ctrl.
jeeswg wrote:So you're remapping the LWin key by itself to an undefined key, although, I suppose LWin when used as a modifier is still working as usual.
"Remapping" requires a key name following the hotkey label. This is just a hotkey. Even in the general sense, "remapping" would imply that the key doesn't still have its native function. This does, because of ~. This does not remap the LWin key; it merely sends another key each time LWin is pressed, which prevents the OS from showing the Start menu, since it only shows the menu if you press and release the key without an intervening event (i.e. key combination).
Hotkeys with the tilde modifier are not intended to block the native function of the key, so in [v1.1.27+] they do not cause masking. Hotkeys like ~#a:: still suppress the menu, since the system detects that the Win key has been used in combination with another key. However, mouse hotkeys and the Win keys themselves (~LWin:: and ~RWin::) do not suppress the Start Menu.

The Start Menu (or the active window's menu bar) can be suppressed by sending any keystroke. The following example disables the ability for LWin to activate the Start Menu, while still allowing its use as a modifier:

Code: Select all

~LWin::Send {Blind}{vk07}
Source: #MenuMaskKey
{Blind} isn't likely to matter since ~LWin:: won't activate if a modifier is held down, but it may reduce the chance of side-effects (such as if the system is under load, allowing a modifier to be pressed after LWin but before Send).
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Blocking the keyboard Windows key

24 Mar 2018, 01:02

I've been using Autohotkey all day with ~LWin::vk07 and there are no problems. It works great, but to be fair, there is no load on the system because everything I do is text, no graphics.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Blocking the keyboard Windows key

24 Mar 2018, 02:28

~LWin::vk07 does more than necessary (in terms of script execution, not keyboard events), and actually uses {Blind}. It is literally equivalent to this:

Code: Select all

*~LWin::
SetKeyDelay -1
Send {Blind}{vk07 DownR}
return

*~LWin up::
SetKeyDelay -1
Send {Blind}{vk07 up}
return
This is as documented, but can also be verified by ListLines and ListHotkeys.
Ghost

Re: Blocking the keyboard Windows key

24 Mar 2018, 12:11

Huhh?
But isn't tilde (~) make it so that the native function is not blocked??
User avatar
TrippReaves
Posts: 1
Joined: 10 Feb 2023, 03:54

Re: Blocking the keyboard Windows key

10 Feb 2023, 04:02

I encountered the same problem, but I have Windows10.
DylanMendos
Posts: 1
Joined: 10 Feb 2023, 04:28

Re: Blocking the keyboard Windows key

10 Feb 2023, 04:46

It is possible to block the Windows key from opening the Start menu while still allowing it to be used in combination with another key.
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Blocking the keyboard Windows key

10 Feb 2023, 05:00

Hallo,
see Note

Note: Microsoft can assign an effect to an unassigned key code at any time.
For example, vk07 was once undefined and safe to use, but since Windows 10 1909 it is reserved for opening the game bar.


Try:

Code: Select all

LWin::vkE8
<#m:: ; LWin + m
MsgBox, % A_ThisHotkey
return
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
RussF
Posts: 1261
Joined: 05 Aug 2021, 06:36

Re: Blocking the keyboard Windows key

10 Feb 2023, 07:29

None of the Windows 10 systems I use (and we have a network of over 50) display the Start Menu until the Windows key is actually released. I can hold it down all day and the Start Menu doesn't come up until it is released. So I'm having a hard time understanding the problem. If you're going to use a Win key combo, the menu should never appear unless you release it without the second combination keypress. This is a non-issue. If you don't want the menu to appear, don't press the Win key by itself.

Russ

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: joedf, OrangeCat, scriptor2016 and 124 guests