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

allow custom key bindings via a config file #49

Open
mokurin000 opened this issue Apr 2, 2023 · 0 comments
Open

allow custom key bindings via a config file #49

mokurin000 opened this issue Apr 2, 2023 · 0 comments

Comments

@mokurin000
Copy link

mokurin000 commented Apr 2, 2023

piano-rs/src/game/notes.rs

Lines 94 to 165 in 2997bee

pub fn key_to_base_note(mut key: KeyEvent, sequence: i8) -> Option<String> {
let mut offset: i8 = 0;
let keys = ['z', 's', 'x', 'c', 'f', 'v', 'g', 'b', 'n',
'j', 'm', 'k', '1', ',', 'q', 'l', '2', '.',
'w', '/', 'e', '\'', '4', 'r', '5', 't', 'y',
'7', 'u', '8', 'i', '9', 'o', 'p', '[', ']', 'a'];
let base_sounds = ["a", "as", "b", "c", "cs", "d", "ds", "e", "f",
"fs", "g", "gs", "gs", "a", "a", "as", "as", "b",
"b", "c", "c", "cs", "cs", "d", "ds", "e", "f",
"fs", "g", "gs", "a", "as", "b", "c", "d", "e", "gs"];
let factors = [-1, -1, -1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, -1];
let special = ['!', '@', '$', '%', '&', '*', '(', '"', '<',
'>', '?', '{', '}'];
let special_matches = ['1', '2', '4', '5', '7', '8', '9', '\'', ',',
'.', '/', '[', ']'];
// Handle terminal control characters
if key == KeyEvent::Enter {
// Ctrl+m sends Enter in terminal
key = KeyEvent::Ctrl('m');
} else if key == KeyEvent::Tab {
// Ctrl+i sends Tab in terminal
key = KeyEvent::Ctrl('i');
}
// Translate Ctrl+<character> to <character>
if let KeyEvent::Ctrl(c) = key {
key = KeyEvent::Char(c);
offset -= 1;
}
// Increment `offset` if key was shift prefixed (Shift+<character>)
let note: Option<String> = if let KeyEvent::Char(mut c) = key {
if c.is_uppercase() {
c = c.to_ascii_lowercase();
offset += 1;
} else if special.contains(&c) {
let j = special.iter()
.position(|&key| key == c)
.unwrap();
c = special_matches[j];
offset += 1;
}
if let Some(i) = keys.iter().position(|&key| key == c) {
let factor = factors[i];
let base_note = format!("{}{}",
base_sounds[i].to_string(),
offset + factor + sequence
);
Some(base_note)
} else {
None
}
} else {
None
};
note
}

  • toml - much more human-friendly config format, I mean not such easy to break like yaml
  • OnceLock for once init of key bind map; may not need to be thread-safe?
  • FxHashMap for keymapping

related issue: #23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant