Core Function KeyHook

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
m (1 revision)
 
(10 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
KeyHook( <name>, <command> )
+
KeyHook( <name>, <command>, <param var> )
 
</pre>
 
</pre>
  
Line 22: Line 22:
  
 
This is similar to Eval().
 
This is similar to Eval().
 +
 +
==== param var ====
 +
 +
Optional; A variable to pass inside the keyhook this variable will become $Param this is most useful for attaching a class to the keyhook so you can access its variables, functions natively.
  
 
=== Return Value ===
 
=== Return Value ===
  
Success: Returns 1.
+
Success: Returns true.
  
Failure: Returns 0.
+
Failure: Returns @PTRZero.
  
 
=== Remarks ===
 
=== Remarks ===
Line 39: Line 43:
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
// Set a new Keyhook called MyHook
 
// Set a new Keyhook called MyHook
KeyHook("MyHook", "myKeyHook();");
+
$hookId = KeyHook("MyHook", 'myKeyHook($Arg, $Key, $KeyExtra);');
 
+
 
// Pressing P will cause the hook to be removed
 
// Pressing P will cause the hook to be removed
 
HotKeySet("p", "removeIt();");
 
HotKeySet("p", "removeIt();");
 
Function removeIt()
 
Function removeIt()
 
{
 
{
KeyHookKill($a);
+
KeyHook("MyHook");
 +
println("Deleted Hook Name 'MyHook'");
 
}
 
}
 
   
 
   
Line 53: Line 58:
 
}
 
}
 
   
 
   
Function myKeyHook()
+
Function myKeyHook($Arg, $Key, $KeyExtra)
 
{
 
{
my List( $Name, $nCode, $wParam, $lParam ) = $arg;
+
my List( $Name, $nCode, $wParam, $lParam ) = $Arg;
 
if($nCode >= 0)
 
if($nCode >= 0)
 
{
 
{
 
if($wParam == 0x0100) // WM_KEYDOWN
 
if($wParam == 0x0100) // WM_KEYDOWN
 
{
 
{
my List( $vkCode, $scanCode, $flags, $time, $dwExtraInfo ) = $key;
+
my List( $VkCode, $ScanCode, $Flags, $Time, $DwExtraInfo ) = $Key;
my $Char = Chr($vkCode);
+
my List( $Char, $Control, $Shift, $Alt, $CapsLock ) = $KeyExtra;
println( "KeyDOWN Hook '$Name' VKCode '$vkCode' Char '$Char'" );
+
println( "KeyDOWN Hook '$Name' Key '$Char' VKCode '$VkCode'" );
 
// If user presses Q remove the hook!
 
// If user presses Q remove the hook!
if($Char == "Q")
+
if($Char== "q")
 
{
 
{
 
// If true is ever returned the hook will be removed
 
// If true is ever returned the hook will be removed
 
println("Deleted Hook Name '$Name'");
 
println("Deleted Hook Name '$Name'");
 
return true;
 
return true;
}
 
// Alternative method to delete
 
// If user presses E remove the hook!
 
else if($Char == "E")
 
{
 
// Setting the hotkey with only 1 param causes it to delete the old one
 
println("Deleted Hook Name '$Name'");
 
KeyHook("MyHook");
 
 
}
 
}
 
}
 
}
 
else if($wParam == 0x0101) // WM_KEYUP
 
else if($wParam == 0x0101) // WM_KEYUP
 
{
 
{
my List( $vkCode, $scanCode, $flags, $time, $dwExtraInfo ) = $key;
+
my List( $VkCode, $ScanCode, $Flags, $Time, $DwExtraInfo ) = $Key;
my $Char = Chr($vkCode);
+
my List( $Char, $Control, $Shift, $Alt, $CapsLock ) = $KeyExtra;
println( "KeyUP Hook '$Name' VKCode '$vkCode' Char '$Char'" );
+
println( "KeyUP Hook '$Name' Key '$Char' VKCode '$VkCode'" );
 
}
 
}
 
}
 
}

Latest revision as of 12:37, 14 June 2015

KeyHook( <name>, <command>, <param var> )

Contents

Description

Places a global system wide Keyhook with callback to a function or command.

Parameters

name

Unique name for the new Keyhook (So it can be removed later!)

If this is the only param used it will try delete a previously made hook by this name.

If a hook already exists with this name it will be replaced with the new code (assuming second param is in use)

command

Optional; A command or function to call etc when the Keyhook event triggers on a keypress up/down etc.

This is similar to Eval().

param var

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

Return Value

Success: Returns true.

Failure: Returns @PTRZero.

Remarks

A keyhook will cause your program to crash if you dont add DoEvents() in your code see below example.

If only the NAME param is used and NOT the command it will add that name to the delete list and the hook of that name (if any exist) will be removed at the earliest opportunity.

Example

// Set a new Keyhook called MyHook
$hookId = KeyHook("MyHook", 'myKeyHook($Arg, $Key, $KeyExtra);');
 
// Pressing P will cause the hook to be removed
HotKeySet("p", "removeIt();");
Function removeIt()
{
	KeyHook("MyHook");
	println("Deleted Hook Name 'MyHook'");
}
 
while(true)
{
	DoEvents();
}
 
Function myKeyHook($Arg, $Key, $KeyExtra)
{
	my List( $Name, $nCode, $wParam, $lParam ) = $Arg;
	if($nCode >= 0)
	{
		if($wParam == 0x0100) // WM_KEYDOWN
		{
			my List( $VkCode, $ScanCode, $Flags, $Time, $DwExtraInfo ) = $Key;
			my List( $Char, $Control, $Shift, $Alt, $CapsLock ) = $KeyExtra;
			println( "KeyDOWN Hook '$Name' Key '$Char' VKCode '$VkCode'" );
			// If user presses Q remove the hook!
			if($Char== "q")
			{
				// If true is ever returned the hook will be removed
				println("Deleted Hook Name '$Name'");
				return true;
			}
		}
		else if($wParam == 0x0101) // WM_KEYUP
		{
			my List( $VkCode, $ScanCode, $Flags, $Time, $DwExtraInfo ) = $Key;
			my List( $Char, $Control, $Shift, $Alt, $CapsLock ) = $KeyExtra;
			println( "KeyUP Hook '$Name' Key '$Char' VKCode '$VkCode'" );
		}
	}
	return false;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox