Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# and Serial Ports Pin
MickCurley22-Dec-07 1:40
MickCurley22-Dec-07 1:40 
GeneralRe: C# and Serial Ports Pin
Matti_P23-Dec-07 23:05
Matti_P23-Dec-07 23:05 
GeneralData GridView Pin
sindhutiwari21-Dec-07 21:38
sindhutiwari21-Dec-07 21:38 
GeneralVC# - How to draw a GIF animation on a picturebox control Pin
Shailesh Appukuttan21-Dec-07 17:02
Shailesh Appukuttan21-Dec-07 17:02 
GeneralRe: VC# - How to draw a GIF animation on a picturebox control Pin
Christian Graus21-Dec-07 17:09
protectorChristian Graus21-Dec-07 17:09 
GeneralRe: VC# - How to draw a GIF animation on a picturebox control Pin
electriac23-Dec-07 3:47
electriac23-Dec-07 3:47 
GeneralRe: VC# - How to draw a GIF animation on a picturebox control Pin
Xmen Real 22-Dec-07 4:13
professional Xmen Real 22-Dec-07 4:13 
GeneralFiring Events in Static Methods Pin
aj.esler21-Dec-07 16:57
aj.esler21-Dec-07 16:57 
Hi.
I am still rather new to C# and realise this might be overly ambitious, but I am trying to create a reusable class that will capture keyboard input. I mostly want it so i can use hotkeys in my apps, but I cant even get a basic version working...

The class is finding the keyboard input ok, ie it will display keys pressed in a message box, so it is working that way.

What i cant get to work is for it to raise an event when a key is pressed (this will later be modified so only when specific combinations are pressed). I think part of the problem is that several of the methods are static, and therefore there is no object that can be associated with the event.

How do i set it up so the event is fired whenever a keypress occurs?

The code i am using is posted below.

<br />
// this is in the form controlling the hook.<br />
public frmControl()<br />
        {<br />
            kHook.KeyPressEvent += new KeyEventHandler(kHook_KeyPress);<br />
        }<br />
<br />
        void kHook_KeyPress(object sender, KeyEventArgs e)<br />
        {<br />
            string keyPressed = ((Keys)e.KeyValue).ToString();<br />
            MessageBox.Show(keyPressed);<br />
        }<br />
<br />
// the actual keyboard hook class<br />
class KeyboardHook<br />
    {<br />
        public bool isHookActive = false;<br />
        private const int WH_KEYBOARD_LL = 13;<br />
        private const int WM_KEYDOWN = 0x0100;<br />
        private static LowLevelKeyboardProc _proc = HookCallback;<br />
        private static IntPtr _hookID = IntPtr.Zero;<br />
<br />
        public KeyboardHook()<br />
        {<br />
        }<br />
<br />
        public void StartKeyboardHook()<br />
        {<br />
            _hookID = SetHook(_proc);<br />
            isHookActive = true;<br />
        }<br />
<br />
        public void StopKeyboardHook()<br />
        {<br />
            UnhookWindowsHookEx(_hookID);<br />
            isHookActive = false;<br />
        }<br />
<br />
        private static IntPtr SetHook(LowLevelKeyboardProc proc)<br />
        {<br />
            using (Process curProcess = Process.GetCurrentProcess())<br />
            using (ProcessModule curModule = curProcess.MainModule)<br />
            {<br />
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc,<br />
                    GetModuleHandle(curModule.ModuleName), 0);<br />
            }<br />
        }<br />
<br />
        private delegate IntPtr LowLevelKeyboardProc(<br />
            int nCode, IntPtr wParam, IntPtr lParam);<br />
<br />
        public static IntPtr HookCallback(<br />
            int nCode, IntPtr wParam, IntPtr lParam)<br />
        {<br />
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)<br />
            {<br />
                int vkCode = Marshal.ReadInt32(lParam);<br />
                //Console.WriteLine((Keys)vkCode);<br />
                //MessageBox.Show(((Keys)vkCode).ToString());<br />
                KHookArgs kh = new KHookArgs(vkCode);<br />
                KeyPressEvent(kh);<br />
            }<br />
            return CallNextHookEx(_hookID, nCode, wParam, lParam);<br />
        }<br />
<br />
        public delegate void KeyPressHandler(KHookArgs kh);<br />
<br />
        public event KeyEventHandler KeyPressEvent;<br />
<br />
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
        private static extern IntPtr SetWindowsHookEx(int idHook,<br />
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);<br />
<br />
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
        [return: MarshalAs(UnmanagedType.Bool)]<br />
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);<br />
<br />
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,<br />
            IntPtr wParam, IntPtr lParam);<br />
<br />
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]<br />
        private static extern IntPtr GetModuleHandle(string lpModuleName);<br />
    }<br />
<br />
    // custom attributes<br />
    public class KHookArgs : EventArgs<br />
    {<br />
        private string keyValue;<br />
<br />
        public KHookArgs(int k)<br />
        {<br />
            keyValue = ((Keys)k).ToString();<br />
        }<br />
<br />
        public string ReturnKey()<br />
        {<br />
            return keyValue;<br />
        }<br />
    }<br />

GeneralRe: Firing Events in Static Methods Pin
Kristian Sixhøj21-Dec-07 20:07
Kristian Sixhøj21-Dec-07 20:07 
GeneralRe: Firing Events in Static Methods Pin
Giorgi Dalakishvili22-Dec-07 4:38
mentorGiorgi Dalakishvili22-Dec-07 4:38 
GeneralLicensing for ASP.NET Pin
Member 183766121-Dec-07 15:28
Member 183766121-Dec-07 15:28 
GeneralRe: Licensing for ASP.NET Pin
Dave Kreskowiak21-Dec-07 15:50
mveDave Kreskowiak21-Dec-07 15:50 
GeneralRe: Licensing for ASP.NET Pin
Paul Conrad22-Dec-07 8:16
professionalPaul Conrad22-Dec-07 8:16 
GeneralRe: Licensing for ASP.NET Pin
Christian Graus21-Dec-07 17:11
protectorChristian Graus21-Dec-07 17:11 
GeneralRe: Licensing for ASP.NET Pin
Expert Coming21-Dec-07 21:03
Expert Coming21-Dec-07 21:03 
GeneralRe: Licensing for ASP.NET Pin
Paul Conrad22-Dec-07 8:18
professionalPaul Conrad22-Dec-07 8:18 
GeneralRe: Licensing for ASP.NET Pin
Jeffrey Walton22-Dec-07 9:15
Jeffrey Walton22-Dec-07 9:15 
GeneralCAB File Extraction Pin
Jeffrey Walton21-Dec-07 13:13
Jeffrey Walton21-Dec-07 13:13 
GeneralRe: CAB File Extraction Pin
Dave Kreskowiak21-Dec-07 14:41
mveDave Kreskowiak21-Dec-07 14:41 
GeneralRe: CAB File Extraction Pin
Jeffrey Walton22-Dec-07 9:11
Jeffrey Walton22-Dec-07 9:11 
GeneralRe: CAB File Extraction Pin
Dave Kreskowiak22-Dec-07 19:12
mveDave Kreskowiak22-Dec-07 19:12 
GeneralClientOnClick and OnClick Pin
MoeInsairat21-Dec-07 10:56
MoeInsairat21-Dec-07 10:56 
GeneralRe: ClientOnClick and OnClick Pin
Christian Graus21-Dec-07 11:31
protectorChristian Graus21-Dec-07 11:31 
GeneralRe: ClientOnClick and OnClick Pin
MoeInsairat21-Dec-07 11:36
MoeInsairat21-Dec-07 11:36 
GeneralRe: ClientOnClick and OnClick Pin
Christian Graus21-Dec-07 17:11
protectorChristian Graus21-Dec-07 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.