Core Function HotKeySet

From Sputnik Wiki
Jump to: navigation, search
HotKeySet( <keydef>, <function>, <param var> )

Contents

Description

Create a custom hotkey to run custom code or execute a function

Parameters

keydef

The key(s) to use as the hotkey. Same format as SendKeys().

function

Optional; The name of the code to execute or the function to call when the key is pressed. Leave blank to unset a previous hotkey.

param var

Optional; A variable to pass inside the hotkey this variable will become $Param this is most useful for attaching a class to the hotkey so you can access its variables, functions natively.

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

There is no limit to simultaneous hotkeys per script that may be registered.

If two Sputnik scripts set the same HotKeys, you should avoid running those scripts simultaneously. (The second script cannot capture the hotkey unless the first script terminates or unregisters the key prior to the second script setting the hotkey.)

Hotkeys can run at the same time as other hotkeys and even other functions.

The following hotkeys cannot be set:

Ctrl+Alt+Delete; It is reserved by Windows F12; It is also reserved by Windows, according to its API. NumPad's Enter Key; Instead, use {Enter} which captures both Enter keys on the keyboard. Win+B,D,E,F,L,M,R,U; and Win+Shift+M These are built-in Windows shortcuts. Note: Win+B and Win+L might only be reserved on Windows XP and above. Alt, Ctrl, Shift, Win These are the modifier keys themselves! Other Any global hotkeys a user has defined using third-party software, any combos of two or more "base keys" such as '{F1}{F2}', and any keys of the form '{LALT}' or '{ALTDOWN}'.


When you set a hotkey, Sputnik captures the keypress and does not pass it on to the active application--with one exception: The Lock keys (NumLock, CapsLock, and ScrollLock) still toggle their respective state! If you wish to send the captured hotkey to the active application you must unregister the hotkey before invoking Send or must use ControlSend:

//capture and pass along a keypress
 
HotKeySet("{Esc}", "captureEsc();");
Function captureEsc()
{
    // ... can do stuff here
    HotKeySet("{Esc}");
    SendKeys("{Esc}");
    HotKeySet("{Esc}", "captureEsc();");
}

This example shows how to get a and b to still type despite being hotkeyed :

// Create the MDI GUI
$GUI = GUICreate("Window", "GUI", 800, 600);
// Show the MDI GUI
GUILoad( $GUI );
 
$B1 = GUICreate("Button", $GUI, "PRESS ME!!!", 8, 8);
GUILink($B1, "Click", 'msgbox("Hello World!");');
 
HotKeySet("a", "lol();");
HotKeySet("b", "lol2();");
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function lol()
{
	println("moo");
    HotKeySet("a");
    SendKeys("a");
    HotKeySet("a", "lol();");
}
 
Function lol2()
{
	println("moo");
    HotKeySet("b");
    SendKeys("b");
    HotKeySet("b", "lol2();");
}

Example

WARNING: To use hotkeys without a GUI you must still have a DoEvents() loop in your script somewhere example

// Make some hotkeys
HotKeySet("a", "Println('You pressed A');");
HotKeySet("b", "Println('You pressed B');");
HotKeySet("c", "Println('You pressed C');");
 
// A loop to keep your script running AND to check for hotkeys
While ( true )
{
	DoEvents(); //Without DoEvents() Sputnik wont know if a hotkey was pressed or not
}
// In GUI programs you dont really need to think about this problem
// Since you use the DoEvents() to keep your GUI working anyway


// Create the MDI GUI
$GUI = GUICreate("Window", "GUI", 800, 600);
// Show the MDI GUI
GUILoad( $GUI );
 
$B1 = GUICreate("Button", $GUI, "PRESS ME!!!", 8, 8);
GUILink($B1, "Click", 'msgbox("Hello World!");');
 
HotKeySet("+!d", "myfunction();");  //Shift-Alt-d
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function myfunction()
{
	println("Hello from the HOTKEY\n");
}

Of course you can make a hotkey execute code directly example:

// Create the MDI GUI
$GUI = GUICreate("Window", "GUI", 800, 600);
// Show the MDI GUI
GUILoad( $GUI );
 
$B1 = GUICreate("Button", $GUI, "PRESS ME!!!", 8, 8);
GUILink($B1, "Click", 'msgbox("Hello World!");');
 
HotKeySet("+!d", 'println("Hello from the HOTKEY\n");');  //Shift-Alt-d
 
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );

Using the param var

Class Test
{
	Function __Construct()
	{
		HotKeySet("a", '$param->foo();', $this);
	}
	Function foo()
	{
		say "foo";
	}
};
 
new Test();
 
while()
doEvents();
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox