Repository: wezterm/wezterm (language: rust)

Issue:
Key repeat gets stuck after pressing two keys in fast succession
### What Operating System(s) are you seeing this problem on?

Linux Wayland

### Which Wayland compositor or X11 Window manager(s) are you using?

Sway

### WezTerm version

20220823-103207-53bc8711

### Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

Yes, and I updated the version box above to show the version of the nightly that I tried

### Describe the bug

The key repeat is stuck when pressing two keys in quick succession resulting in the second key printing only one character instead of continuously printing.

### To Reproduce

Go into a terminal. Press the key `A` on your keyboard and hold it. Release it and quickly press the letter `B` on the keyboard and hold it. `b` is only printed once, even though it should be continuously be printed.

### Configuration

no config

### Expected Behavior

Every key that is pressed and held should be repeated, even when keys are typed in quick succession.

### Logs

There seem to be no logs created from this action

### Anything else?

_No response_


Discussion / hints:
I'm seeing this too, FWIW.
Can reproduce, though cannot imagine what you were doing to discover that during normal usage :)

I think this might be happening because the press of the second key comes before the release of the first one:

```
[2471895.630] wl_keyboard@14.key(18833, 7498288, 48, 1)
[2471902.686] wl_keyboard@14.key(18834, 7498296, 30, 0)
```

Looking at GTK (gedit), any new key press (without releasing the previously held key at all) does interrupt the key repeat :heavy_check_mark: 

Fix patch:
diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs
index f1f87a86f19..d93e0b7e42b 100644
--- a/window/src/os/wayland/window.rs
+++ b/window/src/os/wayland/window.rs
@@ -74,7 +74,7 @@ impl KeyRepeatState {
 
                     let mut inner = handle.borrow_mut();
 
-                    if inner.key_repeat.as_ref().map(|k| Arc::as_ptr(k))
+                    if inner.key_repeat.as_ref().map(|(_, k)| Arc::as_ptr(k))
                         != Some(Arc::as_ptr(&state))
                     {
                         // Key was released and/or some other key is doing
@@ -138,7 +138,7 @@ pub struct WaylandWindowInner {
     hscroll_remainder: f64,
     vscroll_remainder: f64,
     modifiers: Modifiers,
-    key_repeat: Option<Arc<Mutex<KeyRepeatState>>>,
+    key_repeat: Option<(u32, Arc<Mutex<KeyRepeatState>>)>,
     pending_event: Arc<Mutex<PendingEvent>>,
     pending_mouse: Arc<Mutex<PendingMouse>>,
     pending_first_configure: Option<async_channel::Sender<()>>,
@@ -439,10 +439,14 @@ impl WaylandWindowInner {
                         when: Instant::now(),
                         event,
                     }));
-                    self.key_repeat.replace(Arc::clone(&rep));
+                    self.key_repeat.replace((key, Arc::clone(&rep)));
                     KeyRepeatState::schedule(rep, self.window_id);
-                } else {
-                    self.key_repeat.take();
+                } else if let Some((cur_key, _)) = self.key_repeat.as_ref() {
+                    // important to check that it's the same key, because the release of the previously
+                    // repeated key can come right after the press of the newly held key
+                    if *cur_key == key {
+                        self.key_repeat.take();
+                    }
                 }
             }
             WlKeyboardEvent::Modifiers {


Test patch: