Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent loss of focus on Tab/Arrow key press #52

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions addons/godot_xterm/terminal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func _gui_input(event):
):
_native_terminal.sb_reset()

# Prevent focus changing to other inputs when pressing Tab or Arrow keys.
if event.scancode in [KEY_LEFT, KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_TAB]:
accept_event()

_handle_mouse_wheel(event)
_handle_selection(event)

Expand Down
50 changes: 50 additions & 0 deletions test/scenes/multiple_inputs.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://addons/godot_xterm/terminal.gd" type="Script" id=1]

[node name="HBoxContainer" type="HBoxContainer"]
anchor_right = 1.0
anchor_bottom = 1.0

[node name="HBoxContainer" type="HBoxContainer" parent="."]
margin_right = 1024.0
margin_bottom = 600.0
size_flags_horizontal = 3

[node name="TextEdit" type="TextEdit" parent="HBoxContainer"]
margin_right = 338.0
margin_bottom = 600.0
size_flags_horizontal = 3

[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
margin_left = 342.0
margin_right = 681.0
margin_bottom = 600.0
size_flags_horizontal = 3

[node name="TextEdit3" type="TextEdit" parent="HBoxContainer/VBoxContainer"]
margin_right = 339.0
margin_bottom = 197.0
size_flags_vertical = 3

[node name="Terminal" type="Control" parent="HBoxContainer/VBoxContainer"]
margin_top = 201.0
margin_right = 339.0
margin_bottom = 398.0
focus_mode = 2
size_flags_vertical = 3
script = ExtResource( 1 )

[node name="TextEdit2" type="TextEdit" parent="HBoxContainer/VBoxContainer"]
margin_top = 402.0
margin_right = 339.0
margin_bottom = 600.0
size_flags_vertical = 3

[node name="TextEdit2" type="TextEdit" parent="HBoxContainer"]
margin_left = 685.0
margin_right = 1024.0
margin_bottom = 600.0
size_flags_horizontal = 3

[connection signal="data_sent" from="HBoxContainer/VBoxContainer/Terminal" to="HBoxContainer/VBoxContainer/Terminal" method="write"]
42 changes: 42 additions & 0 deletions test/unit/terminal.test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,45 @@ func test_bell_cooldown() -> void:
term.write("\a")
yield(yield_to(term, "bell", 5), YIELD)
assert_signal_emit_count(term, "bell", 2)


class TestMultipleInputs:
# Tests for when Terminal is around other input nodes and arrow keys or TAB
# key is pressed. Focus should not change to other inputs when pressing these
# keys (same behaviour as TextEdit node).
# See: https://github.com/lihop/godot-xterm/issues/51
extends "res://addons/gut/test.gd"

const KEYS := {
KEY_LEFT = KEY_LEFT,
KEY_UP = KEY_UP,
KEY_RIGHT = KEY_RIGHT,
KEY_DOWN = KEY_DOWN,
KEY_TAB = KEY_TAB,
}

var terminal: Control

func press_key(scancode: int, unicode := 0) -> void:
var key_down = InputEventKey.new()
key_down.scancode = scancode
key_down.pressed = true
Input.parse_input_event(key_down)
yield(get_tree().create_timer(0.1), "timeout")
var key_up = InputEventKey.new()
key_up.scancode = scancode
key_up.pressed = false
Input.parse_input_event(key_up)

func before_each():
var scene := preload("../scenes/multiple_inputs.tscn").instance()
add_child_autofree(scene)
terminal = scene.find_node("Terminal")
terminal.grab_focus()

func test_terminal_keeps_focus_when_certain_keys_pressed():
for key in KEYS.keys():
press_key(KEYS[key])
assert_true(
terminal.has_focus(), "Terminal should still have focus after %s is pressed." % key
)