Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

User-defined Dynamic Hotkeys


  • Please log in to reply
34 replies to this topic
JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007
I've been more lucky than jaco0646 when he started this topic. I found a lot of useful info in this thread. Thanks to him and to evilc, I wrote my own version that define hotkeys in a loop and read/save them to an .ini file. The script can be found on the new AHK forum at this address: http://ahkscript.org...c.php?f=6&t=860

Thanks!

(sorry for the duplicate comment - unable to delete one of the tow dupes)

evilc
  • Members
  • 340 posts
  • Last active: Oct 27 2015 11:07 PM
  • Joined: 17 Nov 2005
I think I have made some major progress on this front.
 
Check out this new proof of concept:
 
QTH6P.jpg
; Proof of concept for replacement of Hotkey GUI Item
 
#InstallKeybdHook
#InstallMouseHook
 
EXTRA_KEY_LIST := "{Esc}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}{Media_Play_Pause}"
 
SingleKey := ""
 
ModifierState := {}
lasthk := ""
 
Gui, Add, Edit, Disabled vHotkeyName w250, None
Gui, Add, Button, gBind yp-1 xp+260, Set Hotkey
Gui, Show, Center w350 h50, Keybind Test
return
 
Bind:
Bind()
return
 
Bind(){
global ModifierState
global BindMode
global EXTRA_KEY_LIST
global lasthk
global found
 
found := 0
ModifierState := {ctrl: 0, alt: 0, shift: 0, win: 0}
Gui, 2:Add, text, center, Please press the desired key combination.`n`n Supports most keyboard keys and all mouse buttons. Also Ctrl, Alt, Shift, Win as modifiers or individual keys.`n`nHit Escape to cancel
Gui, 2:-Border +AlwaysOnTop
Gui, 2:Show
 
BindMode := 1
Loop {
Input, SingleKey, L1 M T0.1, %EXTRA_KEY_LIST%
if (ErrorLevel == "Max" ) {
found := SingleKey
} else if (substr(ErrorLevel,1,7) == "EndKey:"){
tmp := substr(ErrorLevel,8)
if (tmp == "Escape"){
break
}
found := tmp
}
if (found){
break
}
}
BindMode := 0
Gui, 2:Submit
if (found){
outstring := ""
outhk := ""
; List modifiers in a specific order
modifiers := ["ctrl","alt","shift","win"]
 
Loop, 4 {
key := modifiers[A_Index]
value := ModifierState[modifiers[A_Index]]
if (value){
if (outstring != ""){
outstring .= " + "
}
stringupper, tmp, key
outstring .= tmp
 
if (key == "ctrl"){
outhk .= "^"
} else if (key == "alt"){
outhk .= "!"
} else if (key == "shift"){
outhk .= "+"
} else if (key == "win"){
outhk .= "#"
}
}
}
if (outstring != ""){
outstring .= " + "
}
outhk .= found
StringUpper, found, found
outstring .= found
 
if (lasthk != ""){
hotkey, ~*%lasthk%, Off
}
lasthk := outhk
hotkey, ~*%lasthk%, DoHotkey
 
GuiControl,, HotkeyName, %outstring%
;msgbox % "Hotkey Detected.`n`nHuman-Readable: " outstring "`nAHK Hotkey string: " outhk
}
return
}
 
DoHotkey:
msgbox You pressed the Hotkey.
return
 
; Detects Modifiers in BindMode
#If BindMode
*lctrl::
*rctrl::
*lalt::
*ralt::
*lshift::
*rshift::
*lwin::
*rwin::
mod := substr(A_ThisHotkey,2)
SetModifier(mod,1)
return
 
*lctrl up::
*rctrl up::
*lalt up::
*ralt up::
*lshift up::
*rshift up::
*lwin up::
*rwin up::
mod := substr(substr(A_ThisHotkey,2),1,strlen(A_ThisHotkey) -4)
SetModifier(mod,0)
if (!ModifierCount()){
;if other modifiers still held, do not set found
found := mod
}
return
 
lbutton::
rbutton::
mbutton::
xbutton1::
xbutton2::
wheelup::
wheeldown::
wheelleft::
wheelright::
soundbeep
found := A_ThisHotkey
return
 
#If
 
; Sets the state of the ModifierState object to reflect the state of the modifier keys
SetModifier(hk,state){
global ModifierState
 
if (hk == "lctrl" || hk == "rctrl"){
ModifierState.ctrl := state
} else if (hk == "lalt" || hk == "ralt"){
ModifierState.alt := state
} else if (hk == "lshift" || hk == "rshift"){
ModifierState.shift := state
} else if (hk == "lwin" || hk == "rwin"){
ModifierState.win := state
}
return
}
 
; Counts how many modifier keys are held
ModifierCount(){
global ModifierState
 
return ModifierState.ctrl + ModifierState.alt + ModifierState.shift  + ModifierState.win
}
The basis is the Input box, to detect all keyboard keys.
The "End Key" feature is used to trap non-printable keys.

Then, conditional hotkeys are used to detect the state of the modifier keys and mouse buttons.

I guess the method used to detect mouse buttons could potentially also be used to detect joystick buttons.

Update: I added this code to my GitHub repo for my ADHD library, as this is where I intend to use the code.
I will continue to refine the system (including trying to add support for joysticks) before merging it in to my library, so I should end up with code that will be usable by anyone.

https://github.com/e...er/bindtest.ahk

evilc
  • Members
  • 340 posts
  • Last active: Oct 27 2015 11:07 PM
  • Joined: 17 Nov 2005

I think I have this nut well and truly cracked now.
 
I have improved upon the original Proof-of-concept, and now have a much more robust version.

The only caveat being that joystick up events and joystick + modifier keys could be supported, but obviously you would need a GetKeyState loop in your code.

For regular keyboard and mouse binding though, this appears to be absolutely bang on.

Here is what I seem to have working:

84dGY.jpg

  • Detects all standard printable and non-printable keyboard keys.
    eg A-Z etc, should support foreign variants (eg with umlaut), Backspace, PrtScr etc
    Numlock  and PrtScr supported but currently disabled
  • Detects many extended keys
    eg Media_Play_Pause
    Easily expanded to add others.
  • Detects all 9 regular mouse button events
    LMB, RMB, MMB, WHEEL U/D/L/R, THUMB BUTTONS 1/2
  • Detects all joystick buttons
    All 16 sticks / 32 buttons
  • Translates all mouse buttons to human readable format
    eg "XButton1" to "MOUSE THUMB 1"
  • Can translate AHK key names to human readable format.
    All number pad keys done.
    Can easily be extended
  • Detects keyboard modifier keys used with any supported other keys
    eg CTRL+ALT+RIGHT MOUSE
  • Detects keyboard modifier keys used in isolation and translates to Human Readable
    eg Hitting just the left Ctrl key shows "LEFT CONTROL"
  • Saves / Loads settings to an INI file
    Stored in script folder
  • Two bindings, but easily extended to any number

The source code is HERE.



evilc
  • Members
  • 340 posts
  • Last active: Oct 27 2015 11:07 PM
  • Joined: 17 Nov 2005

<delteted>



seans
  • Members
  • 4 posts
  • Last active: Jun 21 2014 05:04 PM
  • Joined: 18 Jun 2014

deleted