Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- bool reg = false;
- [DllImport("user32.dll")]
- static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
- [DllImport("user32.dll")]
- static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
- [DllImport("kernel32.dll")]
- static extern uint GetCurrentThreadId();
- public Form1()
- {
- InitializeComponent();
- }
- private HookProc myCallbackDelegate = null;
- private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
- public enum HookType : int
- {
- WH_JOURNALRECORD = 0,
- WH_JOURNALPLAYBACK = 1,
- WH_KEYBOARD = 2,
- WH_GETMESSAGE = 3,
- WH_CALLWNDPROC = 4,
- WH_CBT = 5,
- WH_SYSMSGFILTER = 6,
- WH_MOUSE = 7,
- WH_HARDWARE = 8,
- WH_DEBUG = 9,
- WH_SHELL = 10,
- WH_FOREGROUNDIDLE = 11,
- WH_CALLWNDPROCRET = 12,
- WH_KEYBOARD_LL = 13,
- WH_MOUSE_LL = 14
- }
- private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
- {
- if (code < 0)
- {
- //you need to call CallNextHookEx without further processing
- //and return the value returned by CallNextHookEx
- return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
- }
- // we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
- //Keys keyPressed = (Keys)wParam.ToInt32();
- //Console.WriteLine(keyPressed);
- if (!reg && wParam != IntPtr.Zero)
- {
- this.Text = wParam.ToString()
- + " " + lParam.ToString();
- reg = true;
- }
- //return the value returned by CallNextHookEx
- return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // initialize our delegate
- this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);
- // setup a keyboard hook
- SetWindowsHookEx(HookType.WH_CALLWNDPROC, this.myCallbackDelegate, IntPtr.Zero, (int)GetCurrentThreadId());//AppDomain.GetCurrentThreadId());
- timer1.Start();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- reg = false; // Otherwise the RAM jumps to 100%
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement