Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Keyboard hooks - Problem

Keyboard hooks - Problem

Scheduled Pinned Locked Moved C#
help
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CyberKewl
    wrote on last edited by
    #1

    I have a problem with keyboard hooks - every time i press hold the CTRL+SHIFT key for quite some time, my application will crash with the error message : "An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object reference not set to an instance of an object." I've searched high and low for a solution to this matter but to no avail. Here's the code that i am using, it is meant to disable keys like ALT+TAB, CTRL+ESC, etc: protected delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); [ DllImport( "user32.dll", EntryPoint="SetWindowsHookExA", CharSet=CharSet.Ansi )] protected static extern int SetWindowsHookEx(int idHook , LowLevelKeyboardProcDelegate lpfn, int hMod , int dwThreadId); [ DllImport( "user32.dll")] protected static extern int CallNextHookEx(int hHook,int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); [ DllImport("user32.dll")] protected static extern int UnhookWindowsHookEx(long hhook); const int WH_KEYBOARD_LL = 13; public struct KBDLLHOOKSTRUCT { public int vkCode; int scanCode; public int flags; int time; int dwExtraInfo; } protected int intLLKey = 0; protected 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 KeyboardHook() { intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc), System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(),0); }

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups