简体   繁体   English

如何检测是否按下了变量的键?

[英]How do I detect if the key of a variable is pressed?

I´m making a game that creates a random key that you have to press.我正在制作一个游戏,它会创建一个您必须按下的随机键。 I have an array with the letters from where it takes one randomly.我有一个带有字母的数组,它随机取一个字母。 I want to detect if the key is pressed so I did this:我想检测是否按下了键,所以我这样做了:

if (Input.GetKey(KeyCode.lettre))

but with letter = "A" for example, it will do this:但是以letter = "A"为例,它会这样做:

if (Input.GetKey(KeyCode."A"))

How do I get this?我怎么得到这个?

if (Input.GetKey(KeyCode.A))

GetKey can be called with a string, so you could just do: GetKey可以用字符串调用,所以你可以这样做:

if (Input.GetKey(letter)) // assuming letter is a string

You might need letter to be lowercase for this to work.您可能需要将letter设为小写才能使其正常工作。 See the list of key names here .请参阅此处的键名列表。


Alternatively, you could use Enum.Parse to convert your string to an enum value:或者,您可以使用Enum.Parse将您的字符串转换为枚举值:

if (Input.GetKey((KeyCode)Enum.Parse(typeof(KeyCode), letter)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM