Jump to content

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

[Function] GetPressedKeys([mode])


  • Please log in to reply
6 replies to this topic
Tester
  • Members
  • 48 posts
  • Last active: Feb 07 2011 01:34 PM
  • Joined: 06 Jul 2006
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.

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:
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)

d-man
  • Members
  • 290 posts
  • Last active: Jun 28 2015 09:26 AM
  • Joined: 08 Jun 2006
nice script, thx for sharing it

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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:
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.
)
; [...]
}

Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

ribbs2521
  • Guests
  • Last active:
  • Joined: --
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?

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

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".
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

automaticman
  • Members
  • 658 posts
  • Last active: Nov 20 2012 06:10 PM
  • Joined: 27 Oct 2006

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.

gamax92
  • Members
  • 411 posts
  • Last active: Aug 06 2013 05:00 AM
  • Joined: 05 Dec 2010
Hello AHK Designers.

I got something that you should take a look at.

Tester wrote it.