The below code will block all the keyboard shortcuts. Since i wanted to implement this in my main application. I created a .dll out of the below code.
After i included the .dll as reference to my main application, i started the application.
It is working fine in the begining. Then if i do some other stuffs and then check the shorcut, it is not disabled, which my .dll has to do always, untill the .exe for my main application is terminated.
Why my .dll is not disabling the shortcuts after some time ?
source code for calling .dll from Program.cs :
using DisableHotKeys;
public static class Program
{
public static DisableKeys disableKeysInstance = new DisableKeys();
[STAThread]
static void Main()
{
disableKeysInstance.DisableKeyboardHook();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Screensaver.ScreensaverRef);
}
}
Source code of .dll :
namespace DisableHotKeys
{
public class DisableKeys
{
private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
[DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
private static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);
[DllImport("user32.dll")]
private static extern int UnhookWindowsHookEx(int hHook);
[DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
const int WH_KEYBOARD_LL = 13;
private int intLLKey;
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
}
private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
bool blnEat = false; switch (wParam)
{
case 256:
case 257:
case 260:
case 261:
//Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
((lParam.vkCode == 27) && (lParam.flags == 32)) ||
((lParam.vkCode == 27) && (lParam.flags == 0)) ||
((lParam.vkCode == 91) && (lParam.flags == 1)) ||
((lParam.vkCode == 92) && (lParam.flags == 1)) ||
((true) && (lParam.flags == 32)))
{
blnEat = true;
}
break;
} if (blnEat) return 1; else return CallNextHookEx(0, nCode, wParam, ref lParam);
}
public void DisableKeyboardHook()
{
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc), System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
}
private void ReleaseKeyboardHook()
{
intLLKey = UnhookWindowsHookEx(intLLKey);
}
}
}
What other code does your application do?
When using
WH_KEYBOARD_LLyour hook is required by the system to process the message it gets within a certain time frame (specified in the registry:KEY_CURRENT_USER\Control Panel\Desktop\LowLevelHooksTimeout) – if it fails to do so, the system will simply call the next hook in the chain. So make absolutely certain that whatever other code you have is pumping messages at the highest priority.Try setting the registry value up and test if that helps – if it does you know that your code needs to respond faster to the hook switch.