Event.KeyboardEvent 键盘事件

static function KeyboardEvent (key : string) : Event

Description描述

Create a keyboard event.

创建一个键盘事件。

This is useful when you need to check if a certain key has been pressed - possibly with modifiers. The syntax for the key string is a key name (same as in the Input Manager), optionally prefixed by any number of modifiers:
& = Alternate, ^ = Control, % = Command/Windows key, # = Shift
Examples: &f12 = Alternate + F12, "^[0]" = Control + keypad0

当你需要检查,如果某个键被按下,可能带有组合键这个很有用的,key的字符串是键的名字(同输入管理器一样),选择任意组合键数量的前缀:& = Alternate,^ = Control,% = Command/Windows key, # = Shift

例如:&f12 = Alt + F12, "^[0]" = Ctrl + 数字键0

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void OnGUI() {
		GUILayout.Label("Press Enter To Start Game");
		if (Event.current.Equals(Event.KeyboardEvent("[enter]")))
			Application.LoadLevel(1);

		if (Event.current.Equals(Event.KeyboardEvent("return")))
			print("I said enter, not return - try the keypad");

	}
}
function OnGUI () {
	GUILayout.Label ("Press Enter To Start Game");
	if (Event.current.Equals (Event.KeyboardEvent ("[enter]")))
		Application.LoadLevel (1);
	if (Event.current.Equals (Event.KeyboardEvent ("return")))
		print ("I said enter, not return - try the keypad");
}
最后修改:2010年12月31日 Friday 23:17

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。