Advertisement
Guest User

Untitled

a guest
Jun 15th, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         bool reg = false;
  16.  
  17.         [DllImport("user32.dll")]
  18.         static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
  19.  
  20.         [DllImport("user32.dll")]
  21.         static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  22.  
  23.         [DllImport("kernel32.dll")]
  24.         static extern uint GetCurrentThreadId();
  25.  
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         private HookProc myCallbackDelegate = null;
  32.  
  33.         private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
  34.  
  35.         public enum HookType : int
  36.         {
  37.             WH_JOURNALRECORD = 0,
  38.             WH_JOURNALPLAYBACK = 1,
  39.             WH_KEYBOARD = 2,
  40.             WH_GETMESSAGE = 3,
  41.             WH_CALLWNDPROC = 4,
  42.             WH_CBT = 5,
  43.             WH_SYSMSGFILTER = 6,
  44.             WH_MOUSE = 7,
  45.             WH_HARDWARE = 8,
  46.             WH_DEBUG = 9,
  47.             WH_SHELL = 10,
  48.             WH_FOREGROUNDIDLE = 11,
  49.             WH_CALLWNDPROCRET = 12,
  50.             WH_KEYBOARD_LL = 13,
  51.             WH_MOUSE_LL = 14
  52.         }
  53.  
  54.         private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
  55.         {
  56.             if (code < 0)
  57.             {
  58.                 //you need to call CallNextHookEx without further processing
  59.                 //and return the value returned by CallNextHookEx
  60.                 return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
  61.             }
  62.             // we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
  63.             //Keys keyPressed = (Keys)wParam.ToInt32();
  64.             //Console.WriteLine(keyPressed);
  65.             if (!reg && wParam != IntPtr.Zero)
  66.             {
  67.                 this.Text = wParam.ToString()
  68.                     + " " + lParam.ToString();
  69.                 reg = true;
  70.             }
  71.             //return the value returned by CallNextHookEx
  72.             return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
  73.         }
  74.  
  75.         private void Form1_Load(object sender, EventArgs e)
  76.         {
  77.             // initialize our delegate
  78.             this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);
  79.  
  80.             // setup a keyboard hook
  81.             SetWindowsHookEx(HookType.WH_CALLWNDPROC, this.myCallbackDelegate, IntPtr.Zero, (int)GetCurrentThreadId());//AppDomain.GetCurrentThreadId());
  82.  
  83.             timer1.Start();
  84.         }
  85.  
  86.         private void timer1_Tick(object sender, EventArgs e)
  87.         {
  88.             reg = false; // Otherwise the RAM jumps to 100%
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement