1
votes

I have an issue with my keyboard where I am experiencing pretty bad keyboard chatter on certain keys. I managed to find an autohotkey script that helps to filter key presses on the keyboard, however what it actually does is send the same key repeatedly if it is held down, which presents a problem for me when I use applications that require the key to be held down for certain shortcuts (Maya, XSI etc.)

I tried modifying the SendInput command with the {blind} flag and adding the 'down' modifier to %k_ThisHotkey%, but I could never get it to work properly.

(I added a 'down' for the first sendinput, and did a GetKeyState in order to check if the key was being held down, then did a condition where if the key was released, a SendInput up would execute. However, this did not work as intended, and I am out of ideas.)

Would anyone be willing to give some pointers as to how I could have a proper debouncer script working, while allowing for keys to be held down as well?

Thanks!

; ---- setups
#UseHook On
;Capslock::LShift


; ---- initialize parameters
if (%0% > 0)
    k_TimeThreshold = %1%
else
    k_TimeThreshold = 40
;k_TimeThreshold = 1000  ; for debugging


; ---- initialize variables
k_LastHotkey = 0
k_LastTick := A_TickCount


; ---- Set all keys as hotkeys. See www.asciitable.com
k_ASCII = 33
Loop
{
    Transform, k_char, Chr, %k_ASCII%

    Hotkey, %k_char%, k_KeyPress

        if ((k_char Not In +,^)  AND  (k_ASCII != 123  AND k_ASCII != 125))
    {
        Hotkey, +%k_char%, k_KeyPress       ; shift
        ; Hotkey, ^%k_char%, k_KeyPress     ; control
        ; Hotkey, !%k_char%, k_KeyPress     ; alt
        ; Hotkey, #%k_char%, k_KeyPress     ; win
    }

    if k_ASCII = 64
        k_ASCII = 91
    else if k_ASCII = 126
        break
    else
        k_ASCII++
}
return 

; ---- End of auto-execute section.


; ---- When a key is pressed by the user, send it forward only if it's not a "bounce"
; ---- A "bounce" is defined as: "key is same as last"  AND  "time since last key is very short"
Enter::
Space::
Tab::
Esc::
BS::
Del::
Ins::
Home::
End::
PgUp::
PgDn::
Up::
Down::
Left::
Right::
k_KeyPress:
{
    k_ThisHotkey := A_ThisHotkey        ; grab the current hotkey
    k_ThisTick := A_TickCount       ; grab the current tick
    ElapsedTime := k_ThisTick - k_LastTick  ; time since last hotkey (ticks)

    if (ElapsedTime > k_TimeThreshold  ||  k_ThisHotkey <> k_LastHotkey)
    {
        if k_ThisHotkey In !,#,^,+,{,},Enter,Space,Tab,Esc,BS,Del,Ins,Home,End,PgUp,PgDn,Up,Down,Left,Right
            SendInput {%k_ThisHotkey%}
        else
            SendInput %k_ThisHotkey%
        ;SendInput %ElapsedTime%  ; for debugging
    }

    k_LastHotkey := k_ThisHotkey    ; store the current hotkey for next time
    k_LastTick := k_ThisTick    ; store the current tick for next time
}
return

; ---- other keys that could be filtered (but caused issues on some keyboards)
;LWin::
;RWin::
;LAlt::
;RAlt::
1
How about just a new keyboard? Unless it is one of those expensive gaming ones, keyboards are pretty cheap. I like the challenge though! - Robert Ilbrink
But but but it's a mechanical keyboard :X - sonictk
One of those good old keyboards (with F1 - F24)....? - Robert Ilbrink
Nope, a more modern Filco one :X But at least I got it when it first came out so I'm not a hipster. Right??! - sonictk

1 Answers

0
votes

Here is an idea. Not sure if it solves your problem. Let me know. I can use the Shift to shift letters and there is is clear difference between a bouncy key and a normal key press. I only tested it with Shift, q and a.

#SingleInstance Force
#installKeybdHook
#Persistent

Shift::
q::
a::
keywait, %A_ThisHotkey%
if (A_PriorHotkey = A_ThisHotkey AND A_TimeSincePriorHotkey < 900)  ; bouncy key
{
    Soundbeep, 500, 200
    Return
}
Soundbeep, 2000, 200
return