Jump to content


Photo

Long keypress - hotkeys w/o modifiers


  • Please log in to reply
10 replies to this topic

#1 Bon

Bon
  • Members
  • 23 posts

Posted 19 May 2012 - 06:20 PM

The following is not so much to demonstrate my superior skills as a programmer :roll: , but rather to promote an idea of mine (yes, I actually thought of this myself!): to differentiate between a long press and a normal press of any key. This way, you can make any key a hotkey, without any modifier key. This example makes a long press of Escape close the current window:
$Escape::                                               ; Long press (> 0.5 sec) on Esc closes window
    KeyWait, Escape, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so long press
        PostMessage, 0x112, 0xF060,,, A                 ; ...close window
    Else                                                ; otherwise...
        Send {Esc}                                       ; ...just send Escape
Return

This works quite well, but I found that sometimes you press Esc by mistake, and you wish there was a way to back out. So this is what I came up with:
$Escape::                                               ; Long press (> 0.5 sec) on Esc closes window - but if you change your mind you can keep it pressed for 3 more seconds
    KeyWait, Escape, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so key is still down...
        {
            KeyWait, Escape, T3                         ; Wait no more than 3 more sec for key to be released
            If !ErrorLevel                              ; No timeout, so key was released
                {
                    PostMessage, 0x112, 0xF060,,, A     ; ...so close window        
                    Return
                }
                                                        ; Otherwise,
            KeyWait, Escape                             ; Wait for button to be released
                                                        ; Then do nothing...
            Return
        }
        
    Send {Esc}
Return

And, since I never can let anything be, a version with bells and whistles:
$Escape::                                               ; Long press (> 0.5 sec) on Esc closes window - but if you change your mind you can keep it pressed for 3 more seconds
    KeyWait, Escape, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so key is still down...
        {
            SoundPlay *64                               ; Play an asterisk (Doesn't work for me though!)
            WinGet, X, ProcessName, A
            SplashTextOn,,150,,`nRelease button to close %x%`n`nKeep pressing it to NOT close window...
            KeyWait, Escape, T3                         ; Wait no more than 3 more sec for key to be released
            SplashTextOff
            If !ErrorLevel                              ; No timeout, so key was released
                {
                    PostMessage, 0x112, 0xF060,,, A     ; ...so close window      
                    Return
                }
                                                        ; Otherwise,                
            SoundPlay *64
            KeyWait, Escape                             ; Wait for button to be released
                                                        ; Then do nothing...            
            Return
        }
        
        Send {Esc}
Return


#2 LarryC

LarryC
  • Guests

Posted 22 May 2012 - 12:43 PM

Your superior skills are actually pretty neat.
Thanks

#3 Carrozza

Carrozza
  • Members
  • 199 posts

Posted 23 May 2012 - 09:15 AM

This idea is simple yet powerful.
Thanks!

#4 Xx7

Xx7
  • Members
  • 612 posts

Posted 27 May 2012 - 05:48 PM

Definitely using this, thanks! :D

#5 Xx7

Xx7
  • Members
  • 612 posts

Posted 01 July 2012 - 02:37 AM

Added a nice little GUI for you...

#6 Bon

Bon
  • Members
  • 23 posts

Posted 04 July 2012 - 08:33 AM

Thanks, Xx7!
Now it's looking pretty professional!

#7 G. Sperotto

G. Sperotto
  • Members
  • 467 posts

Posted 04 July 2012 - 01:34 PM

A very nice idea indeed. Simple and powerful.

I think this should actually be added to the documentation. We get a lot of questions on timing keypresses and the usual answers often use A_TickCount, GetKeyState and SetTimer in a way that is rather complicated for begginers.

Congratulations :)

#8 ironcates

ironcates
  • Members
  • 32 posts

Posted 10 July 2012 - 04:23 PM

This is a great idea. I'd like to use this to write ascii characters alternatives to various keys

I'm wondering what I can do to avoid the repeat character after I release.

$Numpad2::                                               ; Long press (> 0.5 sec) on * substitutes the dot multiply
    KeyWait, Numpad2, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel                                       ; timeout, so long press
        SendInput, {ASC 253}							;squared symbol             
    Else                                                ; otherwise...
        Send {Numpad2}                                       ; ...just send Numpad2
Return

Any ideas?
This is what happens when I long press 2 ..... ²2

#9 HotKeyIt

HotKeyIt
  • Fellows
  • 6132 posts

Posted 10 July 2012 - 07:14 PM

:?:
$Numpad2::                                               ; Long press (> 0.5 sec) on * substitutes the dot multiply

    KeyWait, Numpad2, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)

    If ErrorLevel                                       ; timeout, so long press

        SendInput, {ASC 253}                     ;squared symbol             

    Else                                                ; otherwise...

        Send {Numpad2}                                       ; ...just send Numpad2

    [color=#FF0000]KeyWait, Numpad2[/color]

Return
or
$Numpad2::                                               ; Long press (> 0.5 sec) on * substitutes the dot multiply

    KeyWait, Numpad2, T0.5                               ; Wait no more than 0.5 sec for key release (also suppress auto-repeat)

    If ErrorLevel {                                     ; timeout, so long press

        SendInput, {ASC 253}                     ;squared symbol             

        [color=#FF0000]KeyWait, Numpad2[/color]

    } Else                                                ; otherwise...

        Send {Numpad2}                                       ; ...just send Numpad2

Return


#10 ironcates

ironcates
  • Members
  • 32 posts

Posted 11 July 2012 - 03:04 PM

HotKeyIt,
You are awesome²!

#11 ironcates

ironcates
  • Members
  • 32 posts

Posted 11 July 2012 - 06:41 PM

I'm having issues using the closer app with Radial Menu. Does anybody happen to use RM and figure out a workaround? I think it has to do with RM being sort of always active and on top.