C# 键盘记录器API和转换为字符串

C# 键盘记录器API和转换为字符串,c#,keyboard-hook,C#,Keyboard Hook,我正在尝试编写一个简单的键盘记录程序,它将根据黑名单检查键入的单词,并在单词被触发时触发屏幕截图。这是因为我们有一个新的预防议程,我们必须在英国学校使用它来捕捉任何可能的极端主义观点 我一直在从 我使用以下代码作为测试,但我尝试将字符添加到字符串中,以便在用户按下空格键时触发与单词列表的比较。我遇到的问题是我无法将字符转换成字符串。是否可以这样做,以便在等待空格键时将其附加到另一个字符串a class Program { static void Main(string[] args) {

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

我一直在从

我使用以下代码作为测试,但我尝试将字符添加到字符串中,以便在用户按下空格键时触发与单词列表的比较。我遇到的问题是我无法将字符转换成字符串。是否可以这样做,以便在等待空格键时将其附加到另一个字符串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();
        }

    }
你能用这辆车作为缓冲吗

像这样的

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();
}
编辑。 我重写了这个。 捕获空格和enter命令

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;
    }
}

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

您试图将字符串文字“空格”分配给
character.ToString()
,我的注释中也有此错误,无法再编辑

下面是一个代码片段,它将根据枚举而不是字符串检查键代码,如果按下空格,它将调用
HandleComparison
方法,然后清除
StringBuilder

我在这里发现的唯一问题是,按下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);
    }
}

到目前为止,您还尝试过什么,比如将字符附加到数组,或者只要按下的键不等于空格键,就连接到字符串,然后执行比较?您可以按枚举而不是按空格来比较键<代码>如果(character.KeyCode=KeyCode.Space)我在下面添加了一个答案。我遇到的问题与我在原始代码中遇到的问题相同。无法将类型“char”隐式转换为“Keystroke.API.CallbackObjects.KeyPressed”keylogger好的,我知道发生了什么
CreateKeyboardHook
有一个参数,该参数带有一个使用
KeyPressed
的操作。在他们的示例中,字符是按下键的对象,而不是字符。您很幸运,因为该对象有一个方法,该方法应为您提供一个按下字符的字符串。请记住,对于特殊的键,它将有类似于您需要解析的内容。编辑上面的代码以显示这一点。