Skip to content

Commit 1472b5c

Browse files
committed
Fix key press detection bug
1 parent 3539709 commit 1472b5c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: src/Misc/Input.bf

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Nanoforge.Misc
1212
public class Input : ISystem
1313
{
1414
//Input state
15+
private List<KeyState> _lastFrameKeyDownStates = new .()..Resize(349) ~ delete _;
1516
private List<KeyState> _keyDownStates = new .()..Resize(349) ~ delete _;
1617
private List<bool> _mouseDownStates = new .()..Resize(3) ~ delete _;
1718
private float _lastMouseX = 0.0f;
@@ -52,6 +53,21 @@ namespace Nanoforge.Misc
5253

5354
}
5455

56+
[SystemStage(.BeginFrame)]
57+
private void BeginFrame()
58+
{
59+
for (int i in 0..<_keyDownStates.Count)
60+
{
61+
//Set key to repeat if it's held down for two frames
62+
//Note: Ideally this should be handled by WndProcKeyDown already, but in practice it doesn't seem to work. At least on my system the second keydown message can be delayed by 1-2 dozen frames
63+
// That breaks KeyPressed(). Unsure if it's an issue with the system update code, my system, or maybe my keyboard
64+
if (_lastFrameKeyDownStates[i] == .Down && _keyDownStates[i] == .Down)
65+
_keyDownStates[i] = .Repeat;
66+
67+
_lastFrameKeyDownStates[i] = _keyDownStates[i];
68+
}
69+
}
70+
5571
//Called at the end of each frame. Resets input state. Should only be called by the engine core
5672
[SystemStage(.EndFrame)]
5773
private void EndFrame()

0 commit comments

Comments
 (0)