C#-检查是否按下了特定键 [英] C# - Check if specific key is pressed

查看:362
本文介绍了C#-检查是否按下了特定键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查是否按下了某个键,但是出现了以下错误消息:

I'm trying to check if a key is pressed, but I get these Error Message:

错误1名称'键盘'在当前上下文中不存在

Error 1 The name 'Keyboard' does not exist in the current context

错误2当前上下文中不存在名称'Key'

Error 2 The name 'Key' does not exist in the current context

你能告诉我如何解决吗?

Can you tell me how to fix it?

public void Main() 
{
    while(true) 
    {
        if (Keyboard.IsKeyPressed(Key.A)) 
        {
            //...
        }
        return;
    }
}

推荐答案

如果您尝试在Windows Forms应用程序中执行此操作,则可以将这些代码添加到Form的KeyDown事件中(或者您需要使用哪个键事件) ):

If you are trying to do this in a windows forms application, maybe you can add these codes into the Form's KeyDown event(or which key event you need to use):

switch (e.KeyData)
{
    //Detects that you pressed the Up Arrow Key, which key do you need, just 
    //write it here.
    case Keys.Up: 
    //enter code here`
    break;
}

这篇关于C#-检查是否按下了特定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆