2016-11-07 6 views
0

Ich habe einen sehr einfachen Hook-Code gemacht (Ich bin ein Anfänger).Wie man richtig abhebt?

Ich habe den Editor geöffnet und getestet.

Wenn ich irgendeine Taste drücke, ertönt ein Signalton und es wird gedruckt.

Mit Ausnahme der "x" -Taste ist es ein Abschlussschlüssel.

Frage:

Ich will "x" nicht sehen Taste gedruckt. Ich habe gerade das Programm beendet. Was muss ich tun ?

namespace HookingStudy 
{ 
    class HookingClass   
    { 
     private const int WM_KEYDOWN = 0x0100; 
     private static LowLevelKeyboardProc _proc = hookCallBack; 
     private static IntPtr _hookID = IntPtr.Zero;      
     public static void Main() 
     { 
      Beep(1111, 222); 
      _hookID = SetHook(_proc); 
      Application.Run(); 
     } 
     private static IntPtr hookCallBack(int nCode, IntPtr wParam, IntPtr lParam) 
     { 
      if(nCode >= 0 && wParam == (IntPtr) WM_KEYDOWN) 
      { 
       int vkCode = Marshal.ReadInt32(lParam); 
       if(vkCode.ToString() == "88")     // 88 ("x" key) 
       { 
        Beep(7777, 222); 
        UnhookWindowsHookEx(_hookID);  
        Process.GetCurrentProcess().Kill(); 
       } 
       Beep(2222, 55); 
      } 
      return CallNextHookEx(_hookID, nCode, wParam, lParam); 
     } 
     private static IntPtr SetHook(LowLevelKeyboardProc proc) 
     { 
      using(Process curProcess = Process.GetCurrentProcess()) 
      using(ProcessModule curModule = curProcess.MainModule) 
      { 
       return SetWindowsHookEx(13, proc, GetModuleHandle(curModule.ModuleName), 0); 
      }     
     } 
     private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); 
     [DllImport("KERNEL32.DLL")]        
     extern public static void Beep(int freq, int dur);  
     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); 
     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     private static extern bool UnhookWindowsHookEx(IntPtr hhk); 
     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); 
     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr GetModuleHandle(string lpModuleName); 
    } 
} 
+1

'vkCode.ToString() == "88"' lustig ist. – Sinatr

+0

Ja. Zum Glück funktioniert es aber. – Jason

+0

Dann sag bitte, was nicht funktioniert? Wird 'UnhookWindowHookEx' aufgerufen? Sie suchen nicht nach Rückgabewerten. – Sinatr

Antwort

1

Ich will nicht den Terminator x bei Notepad gedruckt sehen

Dann nächste Haken in der Kette nicht nennen:

return CallNextHookEx(_hookID, nCode, wParam, lParam); 

Die Idee, es von Einhaken zu installieren eigene Handler vorherige vorhandene Handler (afair von winapi). Durch abfangen (wie Sie es schon tun) sind Sie nicht nur hören, aber immer noch vorherige Handler mit diesem Anruf aufrufen.

Probieren Sie etwas wie (nicht getestet):

if(vkCode == 88) 
{ 
    ... 
    return 0; 
} 
+0

Danke. Ich werde es ausprobieren. – Jason

+0

Hmmm .. Ich habe "Return (IntPtr) 1;" hinzugefügt. Es sieht nicht schlecht aus. Aber, das Programm selbst läuft noch (hinterher). Wie kann ich es dauerhaft beenden? Vielen Dank. – Jason

+0

* "Programm selbst läuft noch" * - das ist ein anderes Problem. 'Application.Run()' ist das WPF? – Sinatr