Blocking Task Keys
-
Following is the VC++.NET code for the Hook Procedure passed as a parameter for the "SetWindowsHookEx" API call. This code was downloaded from http://msdn.microsoft.com/msdnmag/issues/02/09/CQA/default.aspx Can some one give the equivalent code for the following in C#. I tried a lot to do so but all in vain.
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
{
KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
if (nCode==HC_ACTION)
{
BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) ||
(pkh->vkCode==VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||
(pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN) ||
(pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN))
{
if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
MessageBeep(0);
return 1;
}
}
return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
}Even a sample project which blocks all Task Keys that is Alt+Tab, Ctrl+Esc, Alt+Esc etc.. CHEERS
-
Following is the VC++.NET code for the Hook Procedure passed as a parameter for the "SetWindowsHookEx" API call. This code was downloaded from http://msdn.microsoft.com/msdnmag/issues/02/09/CQA/default.aspx Can some one give the equivalent code for the following in C#. I tried a lot to do so but all in vain.
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
{
KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
if (nCode==HC_ACTION)
{
BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) ||
(pkh->vkCode==VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||
(pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN) ||
(pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN))
{
if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
MessageBeep(0);
return 1;
}
}
return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
}Even a sample project which blocks all Task Keys that is Alt+Tab, Ctrl+Esc, Alt+Esc etc.. CHEERS
You need to P/Invoke the necessary native APIs, such as
SetWindowsHookEx
. There are several articles on this on CodeProject, including Using Hooks from C#[^]. The implementation is pretty much the same, although you'll need to find the values for the various consts which are defined in winuser.h in the Platform SDK. Just define these asconst int
in your code.Microsoft MVP, Visual C# My Articles
-
You need to P/Invoke the necessary native APIs, such as
SetWindowsHookEx
. There are several articles on this on CodeProject, including Using Hooks from C#[^]. The implementation is pretty much the same, although you'll need to find the values for the various consts which are defined in winuser.h in the Platform SDK. Just define these asconst int
in your code.Microsoft MVP, Visual C# My Articles
I have gone through all the articles in CodeProject reagrding hooks. I have understood quite something regarding hooks. The problems i have are: 1) What should the Delegate function should return so as to Block the keypress or not. 2) As given above, in VC++, the author has defined a structure called KBDLLHOOKSTRUCT which he is using to find which key was pressed by type casting LPARAM to this structure. I am not able to do that and hence forth not able to find which keys were pressed. CHEERS
-
I have gone through all the articles in CodeProject reagrding hooks. I have understood quite something regarding hooks. The problems i have are: 1) What should the Delegate function should return so as to Block the keypress or not. 2) As given above, in VC++, the author has defined a structure called KBDLLHOOKSTRUCT which he is using to find which key was pressed by type casting LPARAM to this structure. I am not able to do that and hence forth not able to find which keys were pressed. CHEERS
You don't need to cast it from the
LPARAM
parameter. Just includeref KBDLLHOOKSTRUCT paramName
as the parameter, which automatically marshals the struct correctly. You can find more information about P/Invoke by reading Consuming Unmanaged DLL Functions[^] in the .NET Framework SDK. If you read the C# article and look at the sample source, the author would've implemented everything you need to know. See the Platform SDK for return values and other information in the documentation for theSetWindowsHookEx
API and related documentation.Microsoft MVP, Visual C# My Articles