MacBook Touch Bar causes accidental F-key presses – a workaround for that (Windows)

Post your working scripts, libraries and tools for AHK v1.1 and older
jimhoyle
Posts: 40
Joined: 19 Dec 2015, 15:49

MacBook Touch Bar causes accidental F-key presses – a workaround for that (Windows)

26 Jul 2020, 17:46

If you're like me and you install Windows onto MacBook (Pro) with the Touch Bar, you probably notice that F-keys are all the time triggered accidentally (or the media keys if you have them on by default). So how to cancel out accidental touch bar keypresses? This is one solution (workaround) for that:

Code: Select all

WaitSecForCapslock()
{
	CapsTime=1000 ; timer in ms (actual time is longer due to possible slowness of this code)
	CapsCounter=0
	Pressed=0
	HotKey, Capslock, PressedKey, On
	Loop
	{
		Sleep 10
		CapsCounter+=10
		If (CapsCounter>=CapsTime)
		{
			HotKey, Capslock, PressedKey, Off
			Return 0
		}
		If Pressed
			Return 1
	}
	Return

	PressedKey:
	Pressed=1
	Return
}

*CapsLock::Return

; $ means when Sending the same key as in the hotkey, loop is prevented
;   (the hotkey will not trigger itself when $ is used)
*$F1::
*$F2::
*$F3::
*$F4::
*$F5::
*$F6::
*$F7::
*$F8::
*$F9::
*$F10::
*$F11::
*$F12::
	myModifier=
	GetKeyState, shiftState, Shift
	GetKeyState, controlState, Control
	GetKeyState, winkeyState, LWin
	GetKeyState, altState, Alt
	ThisKeyNow := StrReplace(A_ThisHotkey, "*") ; just remove that *
	ThisKeyNow := StrReplace(ThisKeyNow, "$") ; just remove that $
	If ( controlState == "D" || winkeyState == "D" || altState == "D")
	{
		If ( shiftState == "D" )
			myModifier := "+"
		If ( controlState == "D" )
			myModifier := myModifier . "^"
		If ( winkeyState == "D" )
			myModifier := myModifier . "#"
		If ( altState == "D" )
			myModifier := myModifier . "!"
		Send %myModifier%{%ThisKeyNow%}
		TrayTip %myModifier%%ThisKeyNow%,`n
		Return ; stop here if other modifiers than shift was held down
	}
	If (shiftState == "D")
		myModifier = Shift+
	TrayTip, CapsLock confirms %myModifier%%ThisKeyNow%,(if in 1000ms)
	If WaitSecForCapslock()
	{
		if (myModifier == "Shift+")
		{
			Send +{%ThisKeyNow%}
			TrayTip, Shift+%ThisKeyNow%,`n
		}
		Else
		{
			Send {%ThisKeyNow%}
			TrayTip, %ThisKeyNow%,`n
		}
	}
	Else
		TrayTip, CapsLock confirms %myModifier%%ThisKeyNow%,(timeout)
	Return
The idea is you have to confirm your F-key press with CapsLock (by pressing it after the F-key). You also have to confirm Shift+F-key the same way. With other modifiers, I didn't add the confirmation to them because they are more rare. However this is work in progress so I might improve this later. One problem is that with a hotkey like #F2, it apparently triggers the hotkey, but upon lifting LWin, Start Menu is opened. I couldn't figure out how to fix that (any ideas?).

About CapsLock, it's normal function is just nulled on the code. On my setup I use CapsLock for many purposes (depending on how long I hold CapsLock I get different functions). But that's another topic.
jimhoyle
Posts: 40
Joined: 19 Dec 2015, 15:49

Re: MacBook Touch Bar causes accidental F-key presses – a workaround for that (Windows)

27 Jul 2020, 15:12

Looks like I found a workaround for that specific problem of Start Menu appearing unnecessarily. This is a very ugly fix, but seems to work for now. You have to add this after the If ( winKeyState == "D" ) row:

Code: Select all

	{
		myModifier := myModifier . "#"
		Hotkey, LWin up, WorkaroundStartMenuCancel, On
	}
And also add this after the last Return:

Code: Select all

WorkaroundStartMenuCancel:
	Hotkey, LWin up,, Off
	Send {LWin up}
	Return

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Gewerd_Strauss and 213 guests