Buffer.clear()可以工作,但buffer.pop()不能工作。

huangapple go评论49阅读模式
英文:

Buffer.clear() is working but buffer.pop() isn't

问题

I'm new to Rust and wanted some help because I can't figure out why this simple code is not working.

我是 Rust 新手,因为我无法弄清楚为什么这个简单的代码不起作用而需要帮助。

I'm using rdev library and I'm just trying to store the typed key in a buffer and delete the last letter when I press backspace key.

我正在使用 rdev 库,只是试图将键入的键存储在缓冲区中,并在按下退格键时删除最后一个字母。

What is weird is that the backspace key trigger correctly but nothing happens when pop() method is called, thus I'm wondering what's wrong here?

奇怪的是,退格键触发正确,但在调用 pop() 方法时什么都不发生,因此我想知道这里出了什么问题。

.clear() method seems to work correctly.

.clear() 方法似乎正常工作。

I'm using rdev = "0.5.2"

我正在使用 rdev = "0.5.2"

Thank you for your help regarding this.

谢谢你在这方面的帮助。

Also if you think there is a better way to store the pressed keys let me know. Maybe there's a way to store the key and check if the user deletes not only the last key but maybe also keys that are in the middle of a word, for example.

如果你认为有更好的方法来存储按下的键,请告诉我。也许有一种方法可以存储键并检查用户是否删除了不仅是最后一个键,还可能是在单词中间的键,例如。

英文:

I'm new to Rust and wanted some help because I can't figure out why this simple code is not working.

I'm using rdev library and I'm just trying to store the typed key in a buffer and delete the last letter when I press backspace key.

What is weird is that the backspace key trigger correctly but nothing happen when pop() method is called, thus I'm wondering what's wrong here?

.clear() method seems to work correctly.

I'm using rdev = "0.5.2"

use rdev::{listen, Event, EventType, Key};
use std::sync::{Arc, Mutex};

#[derive(Default)]
struct AppState {
    buffer: String,
}

fn handle_key(app_state: &mut AppState, event: &Event) {            
    if let Some(ref string) = event.name  {
            app_state.buffer.push_str(string);
    }
    else if let EventType::KeyRelease(key) = event.event_type {
        if key == Key::Backspace {
            app_state.buffer.pop();
        }
        if key == Key::Escape {
            app_state.buffer.clear()
        }
        println!("buffer : {}", app_state.buffer);
    }
}

#[tokio::main]
async fn main() {
    let app_state = Arc::new(Mutex::new(AppState::default()));

    let callback = {
        move |event: Event| {
            let mut app_state = app_state.lock().unwrap();
            handle_key(&mut app_state, &event);
        }
    };

    if let Err(error) = listen(callback) {
        println!("Error: {:?}", error)
    }

}

Thank you for you help regarding this.
Also if you think there is a better way to store the pressed keys let me know. Maybe there's a way to store the key and check if the user delete not only the last key but maybe also keys that are in a middle of a word for example.

答案1

得分: 1

Sure, here is the translated code:

实际上 buffer.pop 是有效的,但问题出在退格键上。根据您的逻辑,当按下退格键时,会出现一个带有退格键 (Ascii code 08) 的 KeyPress 事件,首先被插入到缓冲区中。然后,当您释放键时,您的代码单独处理 KeyRelease,这是您弹出那个最后的 08 的地方。

您可以选择在按下退格键时进行双重弹出,或者尝试在按下退格键时避免插入字符串。

双重弹出: 如果您添加第二个 pop 操作,您将得到想要的结果:

   ...
   ...
   if key == Key::Backspace {
      let c = app_state.buffer.pop();
      println!("C: {:#?}", c.unwrap());
      let c = app_state.buffer.pop();
      println!("C: {:#?}", c.unwrap());
    }
   ...
   ...

或者一个更好的选择:

避免将退格键插入到缓冲区中


   ...
   ...
fn handle_key(app_state: &mut AppState, event: &Event) {
  if let Some(ref string) = event.name  {
    if "\u{0008}" != string {
      app_state.buffer.push_str(string);
    }
  }
  ...

第三种方法是重构您的代码,以处理KeyPress 事件,并在字符是可打印字符时插入字符,避免处理其他情况。

英文:

Actually buffer.pop is working, but the problem is with the backspace. According to your logic, when you press backspace, a KeyPress event with backspace (Ascii code 08) arrives and gets inserted to the buffer first. Then when you release the key, your code handles KeyRelease separately and that is where you pop that last 08 back.

You can either double pop with backspace, or try to avoid inserting a string when backspace is pressed.

Double pop: If you add a second pop operation, you will get what you want:

   ...
   ...
   if key == Key::Backspace {
      let c = app_state.buffer.pop();
      println!("C: {:#?}", c.unwrap());
      let c = app_state.buffer.pop();
      println!("C: {:#?}", c.unwrap());
    }
   ...
   ...

Or a better alternative:

Avoid backspace being inserted to the buffer:


   ...
   ...
fn handle_key(app_state: &mut AppState, event: &Event) {
  if let Some(ref string) = event.name  {
    if "\u{0008}" != string {
      app_state.buffer.push_str(string);
    }
  }
  ...

A third approach would be to refactor your code to handle KeyPress as well and insert the character if its a printable character and avoid the rest..

huangapple
  • 本文由 发表于 2023年4月1日 01:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定