Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys, i've faced a problem when trying hook keyboard through warcraft, the SetWindowHookEx seems to be useless in kind of full screen app, can someone help me figure it out? By the way, is there any trick to catch the key when user press one, and send back a key bindings? For example, user press the key A, and we send back shift+ABC. Please hlep me out.

Kind and regards,

Sorry, here is my code. Can you help me figure out why my hook procedure did not work when in full screen game ?

I have as class like this:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using System.ComponentModel;
 
namespace HookApp
{
 
    class Y2KeyboardHook
    {
 
        #region Win32 API Functions and Constants
 
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            KeyboardHookDelegate lpfn, IntPtr hMod, int dwThreadId);
 
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
 
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);
 
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
 
        private const int WH_KEYBOARD_LL = 13;
 
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x101;
 
        #endregion
 
        private KeyboardHookDelegate _hookProc;
        private IntPtr _hookHandle = IntPtr.Zero;
 
        public delegate IntPtr KeyboardHookDelegate(int nCode, IntPtr wParam, IntPtr lParam);
 
        [StructLayout(LayoutKind.Sequential)]
        public struct KeyboardHookStruct
        {
            public int VirtualKeyCode;
            public int ScanCode;
            public int Flags;
            public int Time;
            public int ExtraInfo;
        }
 
        #region Keyboard Events
 
        public event KeyEventHandler KeyDown;
        public event KeyEventHandler KeyUp;
 
        #endregion
 
        // destructor
        ~Y2KeyboardHook()
        {
            Uninstall();
        }
 
        public void Install()
        {
            _hookProc = KeyboardHookProc;
            _hookHandle = SetupHook(_hookProc);
 
            if (_hookHandle == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        private IntPtr SetupHook(KeyboardHookDelegate hookProc)
        {
            IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
 
            return SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);
        }
 
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyboardHookStruct kbStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
 
                if (wParam == (IntPtr)WM_KEYDOWN)
                {
                    if (KeyDown != null)
                        KeyDown(null, new KeyEventArgs((Keys)kbStruct.VirtualKeyCode));
                }
                else if (wParam == (IntPtr)WM_KEYUP)
                {
                    if (KeyUp != null)
                        KeyUp(null, new KeyEventArgs((Keys)kbStruct.VirtualKeyCode));
                }
            }
 
            return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
        }
 
        public void Uninstall()
        {
            UnhookWindowsHookEx(_hookHandle);
        }
 
    }
}


And here is the form:

C#
using System;
using System.Windows.Forms;
using HookApp;
using System.Drawing;
 
namespace WindowsFormApplication1
{
    public partial class Form1 : Form
    {
        Y2KeyboardHook _keyboardHook;
 
        public Form1()
        {
            InitializeComponent();
 
            this.TopMost = true;
 
            ListBox listBox1 = new ListBox();
            listBox1.Location = new Point(10, 10);
            listBox1.Size = new Size(200, 200);
 
            this.Controls.Add(listBox1);
 
            _keyboardHook = new Y2KeyboardHook();
            _keyboardHook.Install();
 
            _keyboardHook.KeyDown += (sender, e) =>
                {
                    listBox1.Items.Add("KeyDown: " + e.KeyCode);
 
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                };
 
            _keyboardHook.KeyUp += (sender, e) =>
                {
                    listBox1.Items.Add("KeyUp: " + e.KeyCode);
 
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                };
        }
 
    }
}
Posted
Updated 16-Apr-13 19:20pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Apr-13 1:03am    
Not true. Who know what did you do? There are many ways to screw up things...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900