AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Function] GetPressedKeys([mode])

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tester



Joined: 06 Jul 2006
Posts: 48
Location: Poland

PostPosted: Mon Jul 10, 2006 12:15 am    Post subject: [Function] GetPressedKeys([mode]) Reply with quote

This function returns string with all pressed keys separated by spaces.
It is usable if you want to check if some extra keys are pressed simultaneously with hotkey key.

Code:
GetPressedKeys(gpk_mode = "")   {
; ____________________________________________________________________
; Usage:   GetPressedKeys([gpk_mode])
;            gpk_mode   - if omitted logical state is given,
;                       if set to "P" physical state is given
; ____________________________________________________________________
; TODO: add ALL missing keys as SCnnn (or maybe better as VKnn?)
; ____________________________________________________________________

   gpk_AllKeyNames := "` 1 2 3 4 5 6 7 8 9 0 - = q w e r t y u i o p [ ] \ a s d f g h j k l `; ' "" z x c v b n m , . / "
      . "LButton RButton MButton XButton1 XButton2 "            ; WheelDown WheelUp
      . "F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24 "
      . "Space Tab Enter Esc Backspace Delete Insert Home End PgUp PgDn Up Down Left Right "
      . "NumpadDiv NumpadMult NumpadAdd NumpadSub NumpadEnter "
      . "NumpadDel NumpadIns NumpadClear NumpadUp NumpadDown NumpadLeft NumpadRight NumpadHome NumpadEnd NumpadPgUp NumpadPgDn "
      . "Numpad0 Numpad1 Numpad2 Numpad3 Numpad4 Numpad5 Numpad6 Numpad7 Numpad8 Numpad9 NumpadDot "
      . "ScrollLock CapsLock NumLock AppsKey "
      . "LWin RWin LControl RControl LShift RShift LAlt RAlt "
      . "PrintScreen CtrlBreak Pause Break "
      . "Help Sleep Browser_Back Browser_Forward Browser_Refresh Browser_Stop Browser_Search Browser_Favorites Browser_Home "
      . "Volume_Mute Volume_Down Volume_Up Media_Next Media_Prev Media_Stop Media_Play_Pause Launch_Mail Launch_Media Launch_App1 Launch_App2"
       
   loop parse, gpk_AllKeyNames, %A_Space%
      if (GetKeyState(A_LoopField, gpk_mode))
         gpk_pressed_keys:=gpk_pressed_keys . " " . A_LoopField

   if (A_AutoTrim = "Off") {
      AutoTrim, On
      gpk_pressed_keys = %gpk_pressed_keys%
      AutoTrim, Off
   }
   else
      gpk_pressed_keys = %gpk_pressed_keys%

   return gpk_pressed_keys
}


Remarks:
- only AHK's documented keys are detected; some keyboard's custom keys shall be added by SCnnn or VKnn codes
- full names of keys are used (Backspace instead of BS) except Esc
- generic/neutral Control, Alt, Shift keys are not present; use Left and Right pairs instead

Example of usage:
Code:
RButton::
   if (GetPressedKeys("P")="RButton")               ; RButton is pressed alone, so simulate clicks
      Send {o down}{LButton down}
   else                                             ; other keys are pressed simultaneously with RButton; so don't simulate custom clicks
      Send {RButton down}
RButton up::
   ...


If anyone have some comments or sugestion, feel free to post it here.

Regards,
Jav!

- update1: I have found that WheelDown and WheelUp keys after use aren't up, so I comment it.
(- update2: performance - coming soon)


Last edited by Tester on Mon Jul 10, 2006 11:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
d-man



Joined: 08 Jun 2006
Posts: 285

PostPosted: Mon Jul 10, 2006 12:39 am    Post subject: Reply with quote

nice script, thx for sharing it

Last edited by d-man on Wed Jul 12, 2006 12:47 am; edited 2 times in total
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Mon Jul 10, 2006 8:22 am    Post subject: Reply with quote

Interesting and useful. Of course, one can shorten the list for better performance.
Note that you could have used continuation section instead of a big string concatenation: it would have been more readable and easier to type (and it is faster, although it has little impact here).
Oh, and it might be interesting to declare it static, in case the function is called frequently. I don't know how static variables are handled in AutoHotkey, I fear that it would evaluate each time if written simplisticly. But a way to be sure it is evaluated only once is to use something like:
Code:
GetPressedKeys(gpk_mode = "")
{
   static gpk_AllKeyNames
   If gpk_AllKeyNames =
      gpk_AllKeyNames =
( Join`s
`` 1 2 3 4 5 6 7 8 9 0 - = q w e r t y u i o p [ ] \ a s d f g h j k l ; ' " z x c v b n m , . /
LButton RButton MButton WheelDown WheelUp XButton1 XButton2
etc.
)
; [...]
}

_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
ribbs2521
Guest





PostPosted: Wed Jul 12, 2006 9:11 am    Post subject: Lost he he Reply with quote

I'm fairly new to this and I'm trying to create some scripts to become more familiar with the commands. I try to run this and it doesn't do anything. I also tried adding FileAppend in hopes that it would send the pressed keys to a .txt file however I cannot figure it out. Anyone wanna help a new guy?
Back to top
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Wed Jul 12, 2006 10:19 am    Post subject: Re: Lost he he Reply with quote

ribbs2521 wrote:
I try to run this and it doesn't do anything.
It is just a function definition, you have to call the function to get the result. See the "Example of usage".
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
automaticman



Joined: 27 Oct 2006
Posts: 642

PostPosted: Sun Dec 19, 2010 6:52 am    Post subject: Reply with quote

PhiLho wrote:
Note that you could have used continuation section instead of a big string concatenation: it would have been more readable and easier to type (and it is faster, although it has little impact here).
How can continuation sections be used to compose Latex codes with AutoHotkey, usually if you just paste any Latex code into such continuation sections it will end up with some error message in AHK.
Back to top
View user's profile Send private message
gamax92



Joined: 05 Dec 2010
Posts: 225

PostPosted: Sun Dec 19, 2010 3:13 pm    Post subject: Reply with quote

Hello AHK Designers.

I got something that you should take a look at.

Tester wrote it.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group