在ubuntu上使用GetAsyncKeyState时出现C#错误

时间:2017-01-24 20:51:59

标签: c# ubuntu

我试图在monodevelop(Ubuntu)中创建一个简单的键盘记录程序,但我一直收到以下错误: The Error that i get

这是我的代码:

class MainClass
{
    [DllImport ("user32.dll")]
    static extern short GetAsyncKeyState(Int32 vKey);
    public static void Main (string[] args)
    {
        MessageBox.Show ("Started");
        LogKeys ();
    }

    static void LogKeys()
    {
        MessageBox.Show ("Called");
        KeysConverter conv = new KeysConverter ();
        String text = "";
        while (true) 
        {
            Thread.Sleep (6);
            for (Int32 i = 0; i < 255; i++) 
            {
                Int32 key = GetAsyncKeyState (i);
                if (key == 1 || key == -32767)
                {
                    text += conv.ConvertToString (i);

                }

            }
        }
    }

}

是因为我使用monodevelop吗?或者是因为我不在实际的Windows机器上?

1 个答案:

答案 0 :(得分:0)

GetASyncKeyState()是user32库中的本机Windows API函数,它不是.NET函数。 MonoDevelop使您可以构建可在linux和Windows上运行的.NET mono应用程序,但您的Linux机器无法访问本机Windows API函数,这就是问题的根源。您将需要找到一种等效的方法来检测.NET中的按键。

GetAsyncKeyState的优点是它是全局的,即使进程的窗口不是目标焦点,它也将检测按键。您需要在.NET中找到类似的功能才能完成相同的任务。