Core Function KeyHook

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 59: Line 59:
 
{
 
{
 
my List( $Name, $nCode, $wParam, $lParam ) = $arg;
 
my List( $Name, $nCode, $wParam, $lParam ) = $arg;
if($wParam == 0x0100) // WM_KEYDOWN
+
if($nCode >= 0)
 
{
 
{
my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
+
if($wParam == 0x0100) // WM_KEYDOWN
my $vkCode = DLLStructGetData($Struct, "vkCode");
+
my $Char = Chr($vkCode);
+
println( "KeyDOWN Hook '$Name' VKCode '$vkCode' Char '$Char'" );
+
// If user presses Q remove the hook!
+
if($Char == "Q")
+
 
{
 
{
// If a value higher than 0 is ever returned the hook will be removed
+
my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
println("Deleted Hook Name '$Name'");
+
my $vkCode = DLLStructGetData($Struct, "vkCode");
return 1;
+
my $Char = Chr($vkCode);
 +
println( "KeyDOWN Hook '$Name' VKCode '$vkCode' Char '$Char'" );
 +
// If user presses Q remove the hook!
 +
if($Char == "Q")
 +
{
 +
// If a value higher than 0 is ever returned the hook will be removed
 +
println("Deleted Hook Name '$Name'");
 +
return 1;
 +
}
 +
// 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");
 +
}
 
}
 
}
// Alternative method to delete
+
else if($wParam == 0x0101) // WM_KEYUP
// 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
+
my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
println("Deleted Hook Name '$Name'");
+
my $vkCode = DLLStructGetData($Struct, "vkCode");
KeyHook("MyHook");
+
my $Char = Chr($vkCode);
 +
println( "KeyUP Hook '$Name' VKCode '$vkCode' Char '$Char'" );
 
}
 
}
}
 
else if($wParam == 0x0101) // WM_KEYUP
 
{
 
my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
 
my $vkCode = DLLStructGetData($Struct, "vkCode");
 
my $Char = Chr($vkCode);
 
println( "KeyUP Hook '$Name' VKCode '$vkCode' Char '$Char'" );
 
 
}
 
}
 
}
 
}

Revision as of 10:15, 30 April 2012

KeyHook( <name>, <command> )

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().

Return Value

Success: Returns 1.

Failure: Returns 0.

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

// Define the Structure that is returned from the Keyhook
DLLStructCreateDef(	"KeyHookStruct", 
			@"
			UInt32 vkCode;
			UInt32 scanCode;
			UInt32 flags;
			UInt32 time;
			PTR dwExtraInfo;
		");
 
// Set a new Keyhook called MyHook
KeyHook("MyHook", "myKeyHook();");
 
while(true)
{
	DoEvents();
}
 
Function myKeyHook()
{
	my List( $Name, $nCode, $wParam, $lParam ) = $arg;
	if($nCode >= 0)
	{
		if($wParam == 0x0100) // WM_KEYDOWN
		{
			my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
			my $vkCode = DLLStructGetData($Struct, "vkCode");
			my $Char = Chr($vkCode);
			println( "KeyDOWN Hook '$Name' VKCode '$vkCode' Char '$Char'" );
			// If user presses Q remove the hook!
			if($Char == "Q")
			{
				// If a value higher than 0 is ever returned the hook will be removed
				println("Deleted Hook Name '$Name'");
				return 1;
			}
			// 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
		{
			my $Struct = PTRToDLLStruct("KeyHookStruct", $LParam);
			my $vkCode = DLLStructGetData($Struct, "vkCode");
			my $Char = Chr($vkCode);
			println( "KeyUP Hook '$Name' VKCode '$vkCode' Char '$Char'" );
		}
	}
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox