Jump to content

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

Sticky Keys


  • Please log in to reply
3 replies to this topic
mr_weed
  • Members
  • 2 posts
  • Last active: Feb 25 2006 04:25 AM
  • Joined: 28 Dec 2005
I posted this over in the "Ask for Help" section the other day: http://www.autohotke...pic.php?p=41836

Maybe someone else might find the script useful...

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
; Sticky v0.01 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;   An extremely basic emulation of MS Windows built-in StickyKeys. 
; 
; * To get a feel for the real MS StickKeys, press the Shift key five times in 
;   a row. 
; 
; Description: Successive presses of modifier keys (shift, control, alt, and 
;              windows) causes a change their functionality, namely... 
;              - 1st press: Waits for another key to be pressed, then sends 
;                           modifier along with last typed key. 
;              - 2nd press: Forces the key to be in a 'down' state until 3rd 
;                           press. 
;              - 3rd press: changes the key back to a normal 'up' state. 
; 
; As per the AHK Help File for 'Send': 
; "Note: Windows does not allow the simulation of the CTRL-ALT-DEL combination" 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

; set hotkey threads to 3... three states for sticky: single, hold on, hold off 
#MaxThreadsPerHotkey 3 

; sound settings 
playSound = True 
highFreq = 440 
lowFreq = 220 
duration = 60 

; key-related variables 
;shiftName = Shift 
shiftKey = + 
shiftState = up 
shiftCount = 0 
;altName = Alt 
altKey = ! 
altState = up 
altCount = 0 
;ctrlName = Ctrl 
ctrlKey = ^ 
ctrlState = up 
ctrlCount = 0 
;winName = LWin 
winKey = # 
winState = up 
winCount = 0 
modifiers = 

; grab input and prepend modifiers as they are needed... 
Loop 
{ 
  Input, key, L1, {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}{ScrollLock}{Enter}{AppsKey}{Sleep} 
  
  ; continue if interrupted by hotkey 
  if ErrorLevel = NewInput 
    Continue 
  ; if in EndKeys, format EndKey for 'Send' command 
  else if InStr(ErrorLevel, "EndKey:") 
  { 
    StringReplace, key, ErrorLevel, EndKey: 
    key = {%key%} 
  } 
  ; send modifiers and key 
  Send, %modifiers%%key% 

  ; remove modifier from list of modifiers 
  if shiftCount = 1 
  { 
    StringReplace, modifiers, modifiers, %shiftKey% 
    shiftCount = 0 
  } 
  if altCount = 1 
  { 
    StringReplace, modifiers, modifiers, %altKey% 
    altCount = 0 
  } 
  if ctrlCount = 1 
  { 
    StringReplace, modifiers, modifiers, %ctrlKey% 
    ctrlCount = 0 
  } 
  if winCount = 1 
  { 
    StringReplace, modifiers, modifiers, %winKey% 
    winCount = 0 
  } 
} 
Return 

;------------------------------------------------------------------------------ 
; StickyKey(keyValue, ByRef keyState, ByRef keyCount) 
; Description: Keeps track of key states and plays system beeps as appropriate. 
;------------------------------------------------------------------------------ 
StickyKey(keyValue, ByRef keyState, ByRef keyCount) 
{ 
  global modifiers, playSound, lowFreq, highFreq, duration 
  keyCount++ 

  ; sticky key in 'up' state 
  if keyState = up 
  { 
    ; 1st press of sticky key: single modifier 
    if keyCount = 1 
    { 
      if playSound 
        SoundBeep, %lowFreq%, %duration% 
      ifnotinstring, modifiers, %keyValue% 
        modifiers = %modifiers%%keyValue% 
    } 

    ; 2nd consecutive press of sticky key: enable hold state 
    else if keyCount = 2 
    { 
      if playSound 
      { 
        SoundBeep, %lowFreq%, %duration% 
        SoundBeep, %highFreq%, %duration% 
      } 
      keyState = down 
    } 
  } 

  ; 3rd press of sticky key: restore to normal mode 
  else 
  { 
    if playSound 
    { 
      SoundBeep, %highFreq%, %duration% 
      SoundBeep, %lowFreq%, %duration% 
    } 
    keyState = up 
    ; remove from modifiers 
    StringReplace, modifiers, modifiers, %keyValue% 
    keyCount = 0 
  } 
  Return 
} 
;------------------------------------------------------------------------------ 
; End Function StickyKey 
;------------------------------------------------------------------------------ 

;============================================================================== 
; Hotkeys 
;============================================================================== 
*$RShift:: 
*$LShift:: 
StickyKey(shiftKey, shiftState, shiftCount) 
Return 

*$RAlt:: 
*$LAlt:: 
StickyKey(altKey, altState, altCount) 
Return 

*$RCtrl:: 
*$LCtrl:: 
StickyKey(ctrlKey, ctrlState, ctrlCount) 
Return 

*$RWin:: 
*$LWin:: 
StickyKey(winKey, winState, winCount) 
Return 
;============================================================================== 
; End Hotkeys 
;============================================================================== 


Tim Tyler
  • Guests
  • Last active:
  • Joined: --
I want this functionality.

However, in this script, Send does not really properly reverse
the effect of Input - and practically /all/ my other AHK hotkeys
are bypassed if I run this script.

I think I'm going to need to find another way.

Tim Tyler
  • Guests
  • Last active:
  • Joined: --

I think I'm going to need to find another way.


Upon further investigation, the WXP OS manages this task well enough.

Tim Tyler
  • Guests
  • Last active:
  • Joined: --
It turns out that the built-in functionality has many problems.

I don't want a 'lock' mode. If I press shift by mistake, I don't
want to have to press it twice more to cancel.

If there is a lock mode, I would like a 'cancel' button.

I only really want a sticky 'caps' key - for capital letters, etc.
I have all my ctrl-key combinations bound to whole keys anyway -
so I rarely press ctrl, and having a ctrl-lock mode is merely an
irritation.

Is there a way to access the 'sticky keys' program via the
command line in some way - or configure its behaviour?