Core Function RealGetKeyState

From Sputnik Wiki
Jump to: navigation, search
RealGetKeyState( <vk_keycode/expression> ) 

Contents

Description

Checks if a key or series of keys is pressed down.

Parameters

vk_keycode/expression

If this param is numeric then a virtual key code (See Macros) is needed.

If this param is a string then it will check all the chars in the string + any specials see remarks.

If this param is a GUIObject of type HotkeyBox then its hotkey will be checked if it is down or not.

If this param is a two element array then [0] will be a key and [1] will be the modifiers it will be checked if it is down or not.

Note - You can add an unlimited number of params

Return Value

Success: Returns 1.

Failure: Returns 0.

Remarks

Since Sputnik uses { } as physical code inside strings you must either escape it like \{ } or place it inside a string type that is not parsed such as 'Test{ }Test' or @"Test{ }Test" etc.

It should be noted this function only check if the key/keys are down when the function is called only it is not the same as HotKeySet() since this only checks when its called and not again.

However you could create a timer or loop to check for keys being pressed creating a kind of Hotkey system the best setting for HotKeys is probably a timer with 100 millisecond interval.

Special symbols to use when the param is a string

! 					Alt Key 
^ 					Control Key
+ 					Shift Key
# 					Windows Key

Special {} to use when the param is a string

{ONCE} 					Must be used by itself this is a special code
					that tells the RealGetKeyState() to only trigger
					if the keys are press ONCE (it will only trigger
					again if the user releases the hotkeys then presses
					the hotkeys again)
					The {ONCE} must be by itself and not part of a large string
{SPACE} 				SPACE 
{ENTER} 				ENTER key on the main keyboard 
{ALT} 					ALT 
{BACKSPACE} or {BS} 			BACKSPACE 
{DELETE} or {DEL} 			DELETE 
{UP} 					Cursor up 
{DOWN} 					Cursor down 
{LEFT} 					Cursor left 
{RIGHT} 				Cursor right 
{HOME} 					HOME 
{END} 					END 
{ESCAPE} or {ESC}			ESCAPE 
{INSERT} or {INS} 			INS 
{PGUP} 					PageUp 
{PGDN} 					PageDown 
{F1} - {F12} 				Function keys 
{TAB} 					TAB 
{PRINTSCREEN} 				Print Screen key 
{LWIN} 					Left Windows key 
{RWIN} 					Right Windows key 
{NUMLOCK} 				NUMLOCK
{CAPSLOCK} 				CAPSLOCK
{SCROLLLOCK} 				SCROLLLOCK
{CTRLBREAK} 				Ctrl+Break 
{PAUSE} 				PAUSE 
{NUMPAD0} - {NUMPAD9} 			Numpad digits 
{NUMPADMULT} 				Numpad Multiply 
{NUMPADADD} 				Numpad Add 
{NUMPADSUB} 				Numpad Subtract 
{NUMPADDIV} 				Numpad Divide 
{NUMPADDOT} 				Numpad period 
{NUMPADENTER} 				Enter key on the numpad 
{APPSKEY} 				Windows App key 
{LALT} 					Left ALT key 
{RALT} 					Right ALT key 
{LCTRL} 				Left CTRL key 
{RCTRL} 				Right CTRL key 
{LSHIFT} 				Left Shift key 
{RSHIFT} 				Right Shift key 
{SLEEP} 				Computer SLEEP key 

Example

Example of using the first param as numeric to scan for a single VK key

println("Try pressing B or C or Shift+A or Shift+Control+D or Shift+Control+Alt+E");
while(true)
{
	if( RealGetKeyState(@KeyB) )
	{
		println("B is pressed");
	}
 
 
	if( RealGetKeyState(@KeyC) )
	{
		println("C is pressed");
	}
 
 
	if( 	RealGetKeyState(@KeyShiftKey) &&
		RealGetKeyState(@KeyA)
		)
	{
		println("Shift+A is pressed");
	}
 
 
	if( 	RealGetKeyState(@KeyShiftKey) &&
		RealGetKeyState(@KeyControlKey) &&
		RealGetKeyState(@KeyD)
		)
	{
		println("Shift+Control+D is pressed");
	}
 
 
	if( 	RealGetKeyState(@KeyShiftKey) &&
		RealGetKeyState(@KeyControlKey) &&
		RealGetKeyState(@KeyMenu) &&
		RealGetKeyState(@KeyE)
		)
	{
		println("Shift+Control+Alt+E is pressed");
	}
 
 
	sleep(100);
}

Example of using the first param as a string to scan for sequence of keys this is same as the above example but using the param as a string instead

println("Try pressing B or C or Shift+A or Shift+Control+D or Shift+Control+Alt+E");
while(true)
{
	if( RealGetKeyState("b") )
	{
		println("B is pressed");
	}
 
 
	if( RealGetKeyState("c") )
	{
		println("C is pressed");
	}
 
 
	if(RealGetKeyState("+a"))
	{
		println("Shift+A is pressed");
	}
 
 
	if(RealGetKeyState("+^d"))
	{
		println("Shift+Control+D is pressed");
	}
 
 
	if(RealGetKeyState("+^!e"))
	{
		println("Shift+Control+Alt+E is pressed");
	}
 
 
	sleep(100);
}

Same example as above but this time using the {ONCE} flag which will make the RealGetKeyState() trigger TRUE only ONCE after hotkeys are pressed then in order to trigger again the user must let go of the hotkeys and then press them again.

println("Try pressing B or C or Shift+A or Shift+Control+D or Shift+Control+Alt+E");
while(true)
{
	if( RealGetKeyState(@"{ONCE}", "b") )
	{
		println("B is pressed");
	}
 
 
	if( RealGetKeyState(@"{ONCE}", "c") )
	{
		println("C is pressed");
	}
 
 
	if(RealGetKeyState(@"{ONCE}", "+a"))
	{
		println("Shift+A is pressed");
	}
 
 
	if(RealGetKeyState(@"{ONCE}", "+^d"))
	{
		println("Shift+Control+D is pressed");
	}
 
 
	if(RealGetKeyState(@"{ONCE}", "+^!e"))
	{
		println("Shift+Control+Alt+E is pressed");
	}
	sleep(1);
}

Another example of using the first param as a string to scan for sequence of keys

while(true)
{
	// Only triggers when SHIFT + NUMLOCK + A are all pressed down
	if( RealGetKeyState( @"+{NUMLOCK}a" ) )
	{
		println("Key sequence was pressed");
	}
	sleep(100);
}

Example of using more than one param

while(true)
{
	if( RealGetKeyState("b", @KeyC) )
	{
		println("B+C is pressed");
	}
	sleep(100);
}

Example of using a HotkeyBox GUI

// Create the GUI
Global $GUI = GUICreate("Window", "Hello", 550, 350);
// Show the GUI
GUILoad( $GUI );
// Create a HotkeyBox for users to type a hotkey they like
Global $Hot = GUICreate("HotkeyBox", $GUI, 8, 8);
// Create a textbox for the user to try out hotkeys
GUICreate("TextBox", $GUI, "", 50, 50);
// Make a timer to check for hotkeys
$Timer = GUICreate("Timer", $GUI, 100);
GUILink($Timer, "Tick", 'ChecForHotkeys();');
GUITimer($Timer, "Start");
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function ChecForHotkeys( )
{
	if(RealGetKeyState($Hot))
	{
		say "Hotkey is pressed";
	}
}

Same as above but this time avoid it spamming the hotkey if its held down

// Create the GUI
Global $GUI = GUICreate("Window", "Hello", 550, 350);
// Show the GUI
GUILoad( $GUI );
// Create a HotkeyBox for users to type a hotkey they like
Global $Hot = GUICreate("HotkeyBox", $GUI, 8, 8);
// Create a textbox for the user to try out hotkeys
GUICreate("TextBox", $GUI, "", 50, 50);
// Make a timer to check for hotkeys
$Timer = GUICreate("Timer", $GUI, 100);
GUILink($Timer, "Tick", 'ChecForHotkeys();');
GUITimer($Timer, "Start");
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function ChecForHotkeys( )
{
	if(RealGetKeyState('{once}', $Hot))
	{
		say "Hotkey is pressed";
	}
}

Example of using an array to check if a key+modifiers is down

// Create the GUI
Global $GUI = GUICreate("Window", "Hello", 550, 350);
// Show the GUI
GUILoad( $GUI );
// Make a timer to check for hotkeys
$Timer = GUICreate("Timer", $GUI, 100);
GUILink($Timer, "Tick", 'ChecForHotkeys();');
GUITimer($Timer, "Start");
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function ChecForHotkeys( )
{
	// Check if Control + A is down using numeric values
	if(RealGetKeyState(array(65, 131072)))
	{
		say "Control + A is down (numeric)";
	}
	// Check if Control + A is down using the @Constants
	if(RealGetKeyState(array(@KeyA, @KeyControl)))
	{
		say "Control + A is down";
	}
	// Check if Control + Shift + A is down using the @Constants
	if(RealGetKeyState(array(@KeyA, @KeyShift | @KeyControl)))
	{
		say "Control + Shift + A is down";
	}
	// Check if Control + Alt + A is down using the @Constants
	if(RealGetKeyState(array(@KeyA, @KeyAlt | @KeyControl)))
	{
		say "Control + Alt + A is down";
	}
	// Check if Control + Shift + Alt + A is down using the @Constants
	if(RealGetKeyState(array(@KeyA, @KeyShift | @KeyAlt | @KeyControl)))
	{
		say "Control + Shift + Alt + A is down";
	}
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox