Skip to content

Commit 16ab468

Browse files
committed
Use is_physical_key_pressed for game input
The documentation of `Input.is_physical_key_pressed` says: > `is_physical_key_pressed()` is recommended over `is_key_pressed()` for > in-game actions, as it will make `W`/`A`/`S`/`D` layouts work regardless of > the user's keyboard layout. `is_physical_key_pressed()` will also ensure that > the top row number keys work on any keyboard layout. If in doubt, use > `is_physical_key_pressed()`. The physical keys labelled `W` `A` `S` `D` on a US keyboard enter different characters on some non-US keyboard layouts: - AZERTY (standard in France): `Z` `Q` `S` `D` - Dvorak (standard on my desk): `,` `A` `O` `E` (The other widely-used layout for Latin text is QWERTZ, used primarily in German-speaking regions; though I have learned while trying to justify this change that the Serbian Cyrillic layout is a rare example of a non-Latin layout that is based on QWERTZ, presumably for geographic reasons. But WASD are the same in QWERTZ and QWERTY.) Adjust the “Move with player 1/2 buttons” blocks accordingly. I have not adjusted the “Move with keys {up: STRING} {down: STRING} {left: STRING} {right: STRING} with speed {speed: VECTOR2}” block because when the user enters strings into that block, they will be entering the logical character corresponding to the physical key on their keyboard that they want to use. It would be very confusing if a French user configured that block to use keys Z S Q D and then those keys were mapped to their physical locations on a US keyboard. The distinction between physical keys and logical keys is confusing and not something we want to try to explain here (IMO).
1 parent d61990c commit 16ab468

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

addons/block_code/examples/pong_game/pong_game.tscn

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
script = ExtResource("5_wr38c")
1717
block_class = &"StatementBlock"
1818
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.835294, 0.262745, 0.133333, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["scope", ""], ["block_format", "Move with player 1 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2()
19-
dir.x += float(Input.is_key_pressed(KEY_D))
20-
dir.x -= float(Input.is_key_pressed(KEY_A))
21-
dir.y += float(Input.is_key_pressed(KEY_S))
22-
dir.y -= float(Input.is_key_pressed(KEY_W))
19+
dir.x += float(Input.is_physical_key_pressed(KEY_D))
20+
dir.x -= float(Input.is_physical_key_pressed(KEY_A))
21+
dir.y += float(Input.is_physical_key_pressed(KEY_S))
22+
dir.y -= float(Input.is_physical_key_pressed(KEY_W))
2323
dir = dir.normalized()
2424
velocity = dir*{speed}
2525
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
@@ -55,10 +55,10 @@ var VAR_DICT := {}
5555
5656
func _process(delta):
5757
var dir = Vector2()
58-
dir.x += float(Input.is_key_pressed(KEY_D))
59-
dir.x -= float(Input.is_key_pressed(KEY_A))
60-
dir.y += float(Input.is_key_pressed(KEY_S))
61-
dir.y -= float(Input.is_key_pressed(KEY_W))
58+
dir.x += float(Input.is_physical_key_pressed(KEY_D))
59+
dir.x -= float(Input.is_physical_key_pressed(KEY_A))
60+
dir.y += float(Input.is_physical_key_pressed(KEY_S))
61+
dir.y -= float(Input.is_physical_key_pressed(KEY_W))
6262
dir = dir.normalized()
6363
velocity = dir*Vector2(0,1000)
6464
move_and_slide()
@@ -69,10 +69,10 @@ func _process(delta):
6969
script = ExtResource("5_wr38c")
7070
block_class = &"StatementBlock"
7171
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.835294, 0.262745, 0.133333, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["scope", ""], ["block_format", "Move with player 2 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2()
72-
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
73-
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
74-
dir.y += float(Input.is_key_pressed(KEY_DOWN))
75-
dir.y -= float(Input.is_key_pressed(KEY_UP))
72+
dir.x += float(Input.is_physical_key_pressed(KEY_RIGHT))
73+
dir.x -= float(Input.is_physical_key_pressed(KEY_LEFT))
74+
dir.y += float(Input.is_physical_key_pressed(KEY_DOWN))
75+
dir.y -= float(Input.is_physical_key_pressed(KEY_UP))
7676
dir = dir.normalized()
7777
velocity = dir*{speed}
7878
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
@@ -108,10 +108,10 @@ var VAR_DICT := {}
108108
109109
func _process(delta):
110110
var dir = Vector2()
111-
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
112-
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
113-
dir.y += float(Input.is_key_pressed(KEY_DOWN))
114-
dir.y -= float(Input.is_key_pressed(KEY_UP))
111+
dir.x += float(Input.is_physical_key_pressed(KEY_RIGHT))
112+
dir.x -= float(Input.is_physical_key_pressed(KEY_LEFT))
113+
dir.y += float(Input.is_physical_key_pressed(KEY_DOWN))
114+
dir.y -= float(Input.is_physical_key_pressed(KEY_UP))
115115
dir = dir.normalized()
116116
velocity = dir*Vector2(0,1000)
117117
move_and_slide()

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ static func get_custom_blocks() -> Array[Block]:
4040
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
4141
b.statement = (
4242
"var dir = Vector2()\n"
43-
+ "dir.x += float(Input.is_key_pressed(KEY_D))\n"
44-
+ "dir.x -= float(Input.is_key_pressed(KEY_A))\n"
45-
+ "dir.y += float(Input.is_key_pressed(KEY_S))\n"
46-
+ "dir.y -= float(Input.is_key_pressed(KEY_W))\n"
43+
+ "dir.x += float(Input.is_physical_key_pressed(KEY_D))\n"
44+
+ "dir.x -= float(Input.is_physical_key_pressed(KEY_A))\n"
45+
+ "dir.y += float(Input.is_physical_key_pressed(KEY_S))\n"
46+
+ "dir.y -= float(Input.is_physical_key_pressed(KEY_W))\n"
4747
+ "dir = dir.normalized()\n"
4848
+ "velocity = dir*{speed}\n"
4949
+ "move_and_slide()"
@@ -56,10 +56,10 @@ static func get_custom_blocks() -> Array[Block]:
5656
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
5757
b.statement = (
5858
"var dir = Vector2()\n"
59-
+ "dir.x += float(Input.is_key_pressed(KEY_RIGHT))\n"
60-
+ "dir.x -= float(Input.is_key_pressed(KEY_LEFT))\n"
61-
+ "dir.y += float(Input.is_key_pressed(KEY_DOWN))\n"
62-
+ "dir.y -= float(Input.is_key_pressed(KEY_UP))\n"
59+
+ "dir.x += float(Input.is_physical_key_pressed(KEY_RIGHT))\n"
60+
+ "dir.x -= float(Input.is_physical_key_pressed(KEY_LEFT))\n"
61+
+ "dir.y += float(Input.is_physical_key_pressed(KEY_DOWN))\n"
62+
+ "dir.y -= float(Input.is_physical_key_pressed(KEY_UP))\n"
6363
+ "dir = dir.normalized()\n"
6464
+ "velocity = dir*{speed}\n"
6565
+ "move_and_slide()"

0 commit comments

Comments
 (0)