KeyDown
How do I use Key Down
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ChatGui.cs
Copy
103 public void OnGUI()
104 {
105 if (!this.IsVisible)
106 {
107 return;
108 }
109
110 GUI.skin.label.wordWrap = true;
111 //GUI.skin.button.richText = true; // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
112 //GUILayout.Button("lala"); // as richText, html tags could be in text
113
114
115 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
116 {
117 if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
118 {
119 // focus on input -> submit it
120 GuiSendsMsg();
121 return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
122 }
123 else
124 {
125 // assign focus to input
126 GUI.FocusControl("ChatInput");
127 }
128 }
129
130 GUI.SetNextControlName("");
131 GUILayout.BeginArea(this.GuiRect);
132
133 GUILayout.FlexibleSpace();
134
135 if (this.chatClient.State != ChatState.ConnectedToFrontEnd)
136 {
137 GUILayout.Label("Not in chat yet.");
138 }
139 else
140 {
141 List
142 int countOfPublicChannels = channels.Count;
143 channels.AddRange(this.chatClient.PrivateChannels.Keys);
144
145 if (channels.Count > 0)
146 {
147 int previouslySelectedChannelIndex = this.selectedChannelIndex;
148 int channelIndex = channels.IndexOf(this.selectedChannelName);
149 this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;
150
151 this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
152 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
153
154 this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
155 this.selectedChannelName = channels[this.selectedChannelIndex];
156
157 if (this.selectedChannelIndex != previouslySelectedChannelIndex)
158 {
159 // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
160 this.scrollPos.y = float.MaxValue;
161 if (this.doingPrivateChat)
162 {
163 string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
164 this.userIdInput = pieces[1];
165 }
166 }
167
168 GUILayout.Label(ChatGui.WelcomeText);
169
170 if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
171 {
172 for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
173 {
174 string sender = this.selectedChannel.Senders[i];
175 object message = this.selectedChannel.Messages[i];
176 GUILayout.Label(string.Format("{0}: {1}", sender, message));
177 }
178 }
179
180 GUILayout.EndScrollView();
181 }
182 }
183
184
185 GUILayout.BeginHorizontal();
186 if (doingPrivateChat)
187 {
188 GUILayout.Label("to:", GUILayout.ExpandWidth(false));
189 GUI.SetNextControlName("WhisperTo");
190 this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
191 string focussed = GUI.GetNameOfFocusedControl();
192 if (focussed.Equals("WhisperTo"))
193 {
194 if (this.userIdInput.Equals("username"))
195 {
196 this.userIdInput = "";
197 }
198 }
199 else if (string.IsNullOrEmpty(this.userIdInput))
200 {
201 this.userIdInput = "username";
202 }
203
204 }
205 GUI.SetNextControlName("ChatInput");
206 inputLine = GUILayout.TextField(inputLine);
207 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
208 {
209 GuiSendsMsg();
210 }
211 GUILayout.EndHorizontal();
212 GUILayout.EndArea();
213 }
File name: NamePickGui.cs
Copy
38 public void OnGUI()
39 {
40 // Enter-Key handling:
41 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
42 {
43 if (!string.IsNullOrEmpty(this.InputLine))
44 {
45 this.StartChat();
46 return;
47 }
48 }
49
50
51 GUI.skin.label.wordWrap = true;
52 GUILayout.BeginArea(guiCenteredRect);
53
54
55 if (this.chatComponent != null && string.IsNullOrEmpty(this.chatComponent.ChatAppId))
56 {
57 GUILayout.Label("To continue, configure your Chat AppId.\nIt's listed in the Chat Dashboard (online).\nStop play-mode and edit:\nScripts/ChatGUI in the Hierarchy.");
58 if (GUILayout.Button("Open Chat Dashboard"))
59 {
60 Application.OpenURL("https://www.exitgames.com/en/Chat/Dashboard");
61 }
62 GUILayout.EndArea();
63 return;
64 }
65
66 GUILayout.Label(this.helpText);
67
68 GUILayout.BeginHorizontal();
69 GUI.SetNextControlName("NameInput");
70 this.InputLine = GUILayout.TextField(this.InputLine);
71 if (GUILayout.Button("Connect", GUILayout.ExpandWidth(false)))
72 {
73 this.StartChat();
74 }
75 GUILayout.EndHorizontal();
76
77 GUILayout.EndArea();
78
79
80 GUI.FocusControl("NameInput");
81 }
File name: PhotonStatsGui.cs
Copy
53 public void Update()
54 {
55 if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
56 {
57 this.statsWindowOn = !this.statsWindowOn;
58 this.statsOn = true; // enable stats when showing the window
59 }
60 }
File name: InRoomChat.cs
Copy
25 public void OnGUI()
26 {
27 if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
28 {
29 return;
30 }
31
32 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
33 {
34 if (!string.IsNullOrEmpty(this.inputLine))
35 {
36 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
37 this.inputLine = "";
38 GUI.FocusControl("");
39 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
40 }
41 else
42 {
43 GUI.FocusControl("ChatInput");
44 }
45 }
46
47 GUI.SetNextControlName("");
48 GUILayout.BeginArea(this.GuiRect);
49
50 scrollPos = GUILayout.BeginScrollView(scrollPos);
51 GUILayout.FlexibleSpace();
52 for (int i = messages.Count - 1; i >= 0; i--)
53 {
54 GUILayout.Label(messages[i]);
55 }
56 GUILayout.EndScrollView();
57
58 GUILayout.BeginHorizontal();
59 GUI.SetNextControlName("ChatInput");
60 inputLine = GUILayout.TextField(inputLine);
61 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
62 {
63 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
64 this.inputLine = "";
65 GUI.FocusControl("");
66 }
67 GUILayout.EndHorizontal();
68 GUILayout.EndArea();
69 }
File name: QuitOnEscapeOrBack.cs
Copy
6 private void Update()
7 {
8 // "back" button of phone equals "Escape". quit app if that's pressed
9 if (Input.GetKeyDown(KeyCode.Escape))
10 {
11 Application.Quit();
12 }
13 }
File name: Game.cs
Copy
125 protected override void Update()
126 {
127 base.Update();
128
129 if (Input.GetKeyDown(KeyCode.Escape))
130 {
131 if (CurrentState == GameState.MainMenu)
132 {
133 Application.Quit();
134 }
135 else
136 {
137 Quit();
138 }
139 }
140 }
File name: GameManager.cs
Copy
172 void Update () {
173
174#if UNITY_EDITOR
175 if (Input.GetKeyDown(KeyCode.Z)) {
176 GameManager.Instance.GameState.Checkmate();
177 GameOver(p1,GameOverType.CHECKMATE); //TODO delete
178 } else if (Input.GetKeyDown(KeyCode.X)) {
179 GameManager.Instance.GameState.Checkmate();
180 GameOver(p2,GameOverType.CHECKMATE); //TODO delete
181 } else if (Input.GetKeyDown(KeyCode.C)) {
182 GameManager.Instance.GameState.Stalemate();
183 GameOver(p2,GameOverType.STALEMATE);
184 }
185#endif
186
187 if (!ready) return;
188 if (gameState.IsGameOver) return;
189
190
191 //EXPERIMENT_TIMER
192 if (whiteTurn) {
193 whiteTimer -= Time.deltaTime;
194 if (whiteTimer < 0 ) {
195 whiteTimer = 0;
196 GameManager.Instance.GameState.OutOfTime();
197 GameOver(p2, GameOverType.OUT_OF_TIME);
198 }
199 UpdateWhiteTimer();
200 } else {
201 blackTimer -= Time.deltaTime;
202 if (blackTimer < 0 ) {
203 blackTimer = 0;
204 GameManager.Instance.GameState.OutOfTime();
205 GameOver(p1, GameOverType.OUT_OF_TIME);
206 }
207 UpdateBlackTimer();
208 }
209 }
File name: PlayerMoveScript.cs
Copy
50 void Update () {
51
52 grounded = Physics2D.OverlapCircle(groundCheck.position, groundChecked, isGround);
53
54 if(transform.position.x > speedCount) {
55 speedCount += speedIncrease;
56 speedIncrease = speedIncrease * speedMultiplier;
57 moveSpeed = moveSpeed * speedMultiplier;
58 }
59
60 rigidbody.velocity = new Vector2 (moveSpeed, rigidbody.velocity.y);
61
62 if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
63
64 if(grounded) {
65 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
66 anim.SetTrigger ("Jump");
67 stopJumping = false;
68 }
69 }
70
71 if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopJumping) {
72 if(jumpTimeCounter > 0) {
73 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
74 jumpTimeCounter -= Time.deltaTime;
75 }
76 }
77
78 if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
79 jumpTimeCounter = 0;
80 stopJumping = true;
81 }
82
83 if(grounded) {
84 jumpTimeCounter = jumpTime;
85 }
86
87
88 scoreCount += pointsPerSeconds = Time.deltaTime;
89
90 if(scoreCount > highScoreCount) {
91 highScoreCount = scoreCount;
92
93 }
94 scoreText.text = "Score: " + Mathf.Round(scoreCount);
95
96 }
File name: GameplayController.cs
Copy
34 void Update () {
35 if(Input.GetKeyDown(KeyCode.Escape)){
36 PausePanel ();
37 }
38 }
File name: MainMenuController.cs
Copy
22 void Update(){
23 if(Input.GetKeyDown(KeyCode.Escape)){
24 if (!quitPanel.activeInHierarchy) {
25 quitPanel.SetActive (true);
26 } else {
27 quitPanel.SetActive (false);
28 }
29 }
30 }
KeyDown 126 lượt xem
Gõ tìm kiếm nhanh...