Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Override key behavior when other keys aren't pressed?


  • Please log in to reply
5 replies to this topic
  • Guests
  • Last active:
  • Joined: --
There's a recent article (<!-- m -->http://stevelosh.com...rn-space-cadet/<!-- m -->) that details a hotkey technique of overriding the default behavior of a modifier key (ctrl, alt, shift...) when it is pressed alone. So if you hit CTRL+C you get the default behavior of exiting a terminal program, or copying text, but if you hit CTRL alone it can trigger a different keypress or launch a script.

The author maps a lone CTRL to ESC (for vim), and maps left and right shift keys to open and closing parentheses.

Is this possible in AHK? How do you remap something with those strict conditions?

GodlyCheese
  • Members
  • 719 posts
  • Last active: Nov 11 2014 07:12 PM
  • Joined: 30 Aug 2012
This is done naturally by AHK, different modifiers can cause different events despite remapping the modifiers. If you want a combination to be changed you must remap that combination. So your request is extremely simple, if you'd tested it yourself you would have seen it:

Ctrl::
Send {esc}
Return

And Ctrl+c will function as it normally does...

  • Guests
  • Last active:
  • Joined: --

This is done naturally by AHK, different modifiers can cause different events despite remapping the modifiers. If you want a combination to be changed you must remap that combination. So your request is extremely simple, if you'd tested it yourself you would have seen it:

Ctrl::
Send {esc}
Return

And Ctrl+c will function as it normally does...


Hey this is great!
Is there a way to do the shift-parens thing though? When i hold a shift key, it repeatedly sends the paren, effectively disabling the shift key as a modifier:

LShift::
send {(}
Return


Gogo
  • Guests
  • Last active:
  • Joined: --
I'm afraid it's not so simple. Here is a script which needs thorough testing.
$LShift::
   Send {LShift down}
   KeyWait LShift
   Send {LShift up}
   If A_PriorKey = LShift
       Send {(}
   return

$RShift::
   Send {RShift down}
   KeyWait RShift
   Send {RShift up}
   If A_PriorKey = RShift
       Send {)}
   return


Gogo
  • Guests
  • Last active:
  • Joined: --
I forgot to say that my script works only with AHK_L

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Works fine for me in AHK Basic with just a few changes:
$LShift::
$RShift::
  Key := SubStr(A_ThisHotkey,2)
  Send, {%Key% Down}
  KeyWait, %Key%
Send, {%Key% Up}
  If ( A_PriorHotkey == A_ThisHotkey )
       SendRaw, % InStr(Key,"L") ? "(" : ")"
Return

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.