0

I want to make a C# Application with a Keyboard and Mouse on screen.
Every Key or Button that is clicked should be seen in this Application by for example coloring one of the keys ( i know how to do that ). This should also work if the Application is not focused.
Currently i am using a global Key- and Mousehook which works fine.
The problem is, the Keyhook does only intercept one Key at a time which means i can only show on Key at a time. I want to be able to show multiple keys at a time on screen. KeyListeners are unfortunately no option because they dont work outside the Application. Does anyone has an idea how to make this possible?

Here's the KeyHook i am using:

public class KeyHook
{
    private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

    static int hHook = 0;

    public static List<Keys> KeyCodes = new List<Keys>();

    const int WH_KEYBOARD_LL = 13;

    HookProc KeyboardHookProcedure;

    [StructLayout(LayoutKind.Sequential)]
    private class keyboardHookStruct
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
    IntPtr hInstance, int threadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern bool UnhookWindowsHookEx(int idHook);

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern int CallNextHookEx(int idHook, int nCode,
    IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string lpFileName);

    public KeyHook()
    {
        Hook();
    }

    ~KeyHook()
    {
        UnHook();
    }

    public int Hook()
    {
        KeyboardHookProcedure = new HookProc(KeyHook.KeyboardHookProc);

        hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, (IntPtr)LoadLibrary("User32"), 0);
        return hHook;
    }

    public bool UnHook()
    {
        bool ret = UnhookWindowsHookEx(hHook);
        if (ret)
            hHook = 0;
        return ret;
    }

    private static int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode < 0)
        {
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        else
        {

            if (((int)wParam == 256) || ((int)wParam == 260))
            {
                keyboardHookStruct MyKeyboardHookStruct = (keyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(keyboardHookStruct));
                //Adding Key to a log i use for other stuff
                KeyCodes.Add((Keys)MyKeyboardHookStruct.vkCode);
                //Code for coloring Key in the UI for pressed Key
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Furious Gamer
  • 359
  • 1
  • 3
  • 16
  • Very curious that you're programming with C# but using old Win32 stuff that you could as easily have coded with C++ a decade ago. – John Saunders Mar 15 '13 at 00:00
  • Well to be honest, i am not that experienced when it comes to C++ and this was just something i came up with after researching – Furious Gamer Mar 15 '13 at 00:06
  • "The problem is, the Keyhook does only intercept one Key at a time which means i can only show on Key at a time. I want to be able to show multiple keys at a time on screen." Can you not keep track of which keys are held down and show all of them at once, despite the hook intercepting only one key at a time? – Sam Mar 15 '13 at 00:19
  • "researching", or poking around on the Internet? How old is this code you found? Be careful that you're not finding ancient code and thinking it's the right way to do things. – John Saunders Mar 15 '13 at 00:21
  • @Sam maybe i can but somehow there doesn't come anything to my mind.. – Furious Gamer Mar 15 '13 at 00:31
  • @John i never said it's a clean or good solution, it just had to work in the beginning – Furious Gamer Mar 15 '13 at 00:32
  • You are only using MyKeyboardHookStruct.vkCode. You get more information in that structure, bit 7 of the flags member tells you whether the key went up or down. – Hans Passant Mar 15 '13 at 00:37
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 15 '13 at 00:53
  • @Hans Thanks but could you be a bit more specific in a way how to access this? I tried a few things but i can't really get the right information.Thanks John – Furious Gamer Mar 15 '13 at 07:19

0 Answers0