2016-12-05 36 views
1

我正在尝试编写一个简单的键盘记录,它将检查输入的黑名单中的单词,并在单词被触发时触发屏幕截图。这是因为我们有一个新的预防议程,我们必须在英国学校中使用,以捕捉任何可能的极端主义观点。键盘记录API和转换为字符串

我已经从https://github.com/fabriciorissetto/KeystrokeAPI

看键盘记录API我用下面的代码的测试,但我想要的字符添加到字符串,所以我可以再火用的比较用户按下空格键时的单词列表。我遇到的麻烦是我无法将字符转换为字符串。是否有可能这样做,以便我可以将它附加到另一个字符串a,同时等待空格键?

class Program 
{ 
    static void Main(string[] args) 
{ 
    using (var api = new KeystrokeAPI()) 
    { 
     api.CreateKeyboardHook((character) => { Console.Write(character); }); 
     Application.Run(); 
    } 
} 
} 

这是我到目前为止,我得到的错误是在if语句转换字符的字符串。

static void Main(string[] args) 
    { 

     string line = ""; 

     using (var api = new KeystrokeAPI()) 
     {        
      api.CreateKeyboardHook((character) => { 

       line += character.ToString(); 

       if (character.ToString() = "space") 
       { 
        Console.Write("Spacebar Hit"); 
       } 

       Console.Write(character.KeyCode); 

      }); 


      Application.Run(); 
     } 

    } 
+1

还有什么你试过到目前为止,任何诸如追加字符到一个数组,或者只要串联到一个字符串作为按键不等于空格键,然后执行你的比较? – ColinM

+2

您可以按枚举比较键,而不是空格。 'if(character.KeyCode = KeyCode.Space)' – ColinM

+0

我在下面添加了一个答案。 – CathalMF

回答

0

你可以使用StringBuilder作为缓冲?

像这样

var buffer = new StringBuilder(); 

using (var api = new KeystrokeAPI()) 
{ 
    api.CreateKeyboardHook((character) => { 
     if (character.ToString() == " ") 
     { 
      //check the word 
      CallSomeMethodToCheckWord(buffer.ToString()); 

      //reset the buffer 
      buffer = new StringBuilder(); 
     } 
     else 
     { 
      //ToString returns special characters in it, so you could append here and parse later, or parse here. 
      buffer.Append(character.ToString()); 
     } 
    }); 
    Application.Run(); 
} 
+0

我得到的问题与我在原始代码中得到的问题相同。不能隐式地将类型'char'转换为'Keystroke.API.CallbackObjects.KeyPressed'\t KeyLogger – Nathan

+1

好的,我看到发生了什么。 'CreateKeyboardHook'有一个使用'KeyPressed'的参数。在他们的例子中,character是一个KeyPressed对象,而不是char。你很幸运,因为该对象有一个方法[ToString()](https://github.com/fabriciorissetto/KeystrokeAPI/blob/master/Keystroke.API/Entities/CallbackObjects/KeyPressed.cs)应该给你一个按下字符的字符串。请记住,对于特殊键,它将具有您需要解析的之类的内容。编辑上面的代码来展示这一点。 – Josh

1

编辑。 我重写了这个。 捕获两种存储空间,并输入命令

static void Main(string[] args) 
{ 
    string line = string.Empty; 

    using (var api = new KeystrokeAPI()) 
    { 
     api.CreateKeyboardHook((character) => { 

      if (character.KeyCode.ToString() == "Space" || character.KeyCode.ToString() == "Return") 
      { 
       if(BannedWordsCheck(line)) 
       { 
        Console.WriteLine("Banned Word Typed: " + line); 
       } 

       line = string.Empty; 
      } 
      else 
      { 
       line += character.KeyCode.ToString(); 
      } 
     }); 

     Application.Run(); 
    } 
} 

static bool BannedWordsCheck(string word) 
{ 
    if(word.ToLower().Contains("terror")) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
1

您在代码中收到的错误是由于以下行 if (character.ToString() = "space")

您正在尝试“空间”来分配字符串文本character.ToString(),我也在我的评论中有这个错误,我不能再编辑了。

这里有一个片段,将检查重点代码对枚举而不是字符串,它会再调用HandleComparison方法,如果空间被按下,然后清除掉StringBuilder

我发现这里是唯一的问题按Shift键会在字符串的前面加上<shift>,所以一些额外的逻辑必须应用于操作键,但这是让您开始使用有效代码示例的基础。

我希望这会有所帮助。

class Program 
{ 
    private static StringBuilder builder; 
    static void Main(string[] args) 
    { 
     using (var api = new KeystrokeAPI()) 
     { 
      builder = new StringBuilder(); 
      api.CreateKeyboardHook(HandleKeyPress); 
      Application.Run(); 
     } 
    } 

    private static void HandleKeyPress(KeyPressed obj) 
    { 
     // To be more reliable, lets use the KeyCode enum instead 
     if (obj.KeyCode == KeyCode.Space) 
     { 
      // Spacebar was pressed, let's check the word and flush the StringBuilder 
      HandleComparison(builder.ToString()); 
      builder.Clear(); 

      return; 
     } 
     // Space wasn't pressed, let's add the word to the StringBuilder 
     builder.Append(obj); 
    } 
    // Handle comparison logic here, I.E check word if exists on blacklist 
    private static void HandleComparison(string compareString) 
    { 
     Console.WriteLine(compareString); 
    } 
}