From d7902db240adc2f081bee7e8fc36b866976fa5d1 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 28 Jul 2008 22:56:12 +0000 Subject: modifier keys for binds --- src/client/input.cc | 46 ++++++++++++++-------- src/client/keyboard.cc | 105 +++++++++++++++++++++++++++++++++++-------------- src/client/keyboard.h | 5 +-- 3 files changed, 106 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/client/input.cc b/src/client/input.cc index d26c3ba..aaf83fa 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -129,8 +129,8 @@ void func_list_keys(std::string const &args) std::stringstream argstr(args); std::string keyname; if (argstr >> keyname) { - aux::lowercase(keyname); - keyboard->list_bind(keyname); + aux::to_lowercase(keyname); + keyboard->bind(keyname, ""); } else { keyboard->list_keys(); } @@ -145,8 +145,8 @@ void func_list_binds(std::string const &args) std::stringstream argstr(args); std::string keyname; if (argstr >> keyname) { - aux::lowercase(keyname); - keyboard->list_bind(keyname); + aux::to_lowercase(keyname); + keyboard->bind(keyname, ""); } else { keyboard->list_binds(); } @@ -161,17 +161,17 @@ void func_bind(std::string const &args) std::stringstream argstr(args); std::string keyname; if (argstr >> keyname) { - aux::lowercase(keyname); - if (args.size() > keyname.size()+1) { + aux::to_lowercase(keyname); + if (args.size() > keyname.size()+1) { keyboard->bind(keyname, args.substr(keyname.size()+1)); } else { - keyboard->list_bind(keyname); + keyboard->bind(keyname, ""); } return; + } else { + core::Func *func = core::Func::find("bind"); + con_print << "Usage: bind " << func->info() << std::endl; } - core::Func *func = core::Func::find("bind"); - con_print << "bind " << func->info() << std::endl; - } else { con_warn << "Keyboard handler not installed!" << std::endl; } @@ -390,6 +390,19 @@ void action_release(std::string const &action) } } +Key::Modifier convert_SDL_modifier(int const sdlmodifier) +{ + if (sdlmodifier & Key::Shift) + return Key::Shift; + else if (sdlmodifier & Key::Ctrl) + return Key::Ctrl; + else if (sdlmodifier & Key::Alt) + return Key::Alt; + else + return Key::None; + +} + void key_pressed(Key *key) { @@ -419,25 +432,24 @@ void key_pressed(Key *key) targets::select_target(targets::hover()); } else { - // FIXME modifiers - char c = key->bind(Key::None).c_str()[0]; + + char c = key->bind(convert_SDL_modifier(keyboard_modifiers)).c_str()[0]; if (c == '+') { // action bind - action_press(key->bind(Key::None)); + action_press(key->bind(convert_SDL_modifier(keyboard_modifiers))); } else if (c) { // normal bind - core::cmd() << key->bind(Key::None) << "\n"; + core::cmd() << key->bind(convert_SDL_modifier(keyboard_modifiers)) << "\n"; } } } else if (core::application()->connected()) { - // FIXME modifiers - char c = key->bind(Key::None).c_str()[0]; + char c = key->bind(convert_SDL_modifier(keyboard_modifiers)).c_str()[0]; if (c && c != '+') { // normal bind - core::cmd() << key->bind(Key::None) << "\n"; + core::cmd() << key->bind(convert_SDL_modifier(keyboard_modifiers)) << "\n"; } } diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index 5870e06..e0c5642 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -245,6 +245,15 @@ void Keyboard::save_binds() if (key->bind(Key::None).size()) { ofs << "bind " << key->name() << " " << key->bind(Key::None) << std::endl; } + if (key->bind(Key::Shift).size()) { + ofs << "bind shift+" << key->name() << " " << key->bind(Key::Shift) << std::endl; + } + if (key->bind(Key::Ctrl).size()) { + ofs << "bind ctrl+" << key->name() << " " << key->bind(Key::Ctrl) << std::endl; + } + if (key->bind(Key::Alt).size()) { + ofs << "bind alt+" << key->name() << " " << key->bind(Key::Alt) << std::endl; + } /* } else { ofs << "unbind " << key->name() << std::endl; @@ -329,19 +338,78 @@ Key *Keyboard::find(unsigned int keysym) void Keyboard::bind(std::string const &name, const std::string str) { - Key *key = find(name); + Key::Modifier modifier = Key::None; + std::string keyname(name); + + if ((keyname.size() > 6) && (keyname.substr(0, 6).compare("shift+") == 0)) { + keyname.erase(0, 6); + modifier = Key::Shift; + } else if ((keyname.size() > 5) && (keyname.substr(0, 5).compare("ctrl+") == 0)) { + keyname.erase(0, 5); + modifier = Key::Ctrl; + } else if ((keyname.size() > 4) && (keyname.substr(0, 4).compare("alt+") == 0)) { + keyname.erase(0, 4); + modifier = Key::Shift; + + } else if ((keyname.size() > 6) && (keyname.substr(keyname.size() - 6, 6).compare("+shift") == 0)) { + keyname.erase(keyname.size() - 6, 6); + modifier = Key::Shift; + } else if ((keyname.size() > 5) && (keyname.substr(keyname.size() - 5, 5).compare("+ctrl") == 0)) { + keyname.erase(keyname.size() - 5, 5); + modifier = Key::Ctrl; + } else if ((keyname.size() > 4) && (keyname.substr(keyname.size() - 4, 4).compare("+alt") == 0)) { + keyname.erase(0, keyname.size() -46); + modifier = Key::Shift; + } + + Key *key = find(keyname); if (key) { - key->assign(Key::None, str.c_str()); + if (str.size()) + key->assign(modifier, str.c_str()); + + if (modifier == Key::None) { + con_print << " " << aux::pad_right(key->name(), 6) << " " << key->bind(Key::None) << std::endl; + } else if (modifier == Key::Shift) { + con_print << " shift+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Shift) << std::endl; + } else if (modifier == Key::Ctrl) { + con_print << " ctrl+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Ctrl) << std::endl; + } else if (modifier == Key::Alt) { + con_print << " alt+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Alt) << std::endl; + } } else { con_print << "Key '" << name << "' not found." << std::endl; } } void Keyboard::unbind(std::string const &name) -{ +{ + Key::Modifier modifier = Key::None; + std::string keyname(name); + + if ((keyname.size() > 6) && (keyname.substr(0, 6).compare("shift+") == 0)) { + keyname.erase(0, 6); + modifier = Key::Shift; + } else if ((keyname.size() > 5) && (keyname.substr(0, 5).compare("ctrl+") == 0)) { + keyname.erase(0, 5); + modifier = Key::Ctrl; + } else if ((keyname.size() > 4) && (keyname.substr(0, 4).compare("alt+") == 0)) { + keyname.erase(0, 4); + modifier = Key::Shift; + + } else if ((keyname.size() > 6) && (keyname.substr(keyname.size() - 6, 6).compare("+shift") == 0)) { + keyname.erase(keyname.size() - 6, 6); + modifier = Key::Shift; + } else if ((keyname.size() > 5) && (keyname.substr(keyname.size() - 5, 5).compare("+ctrl") == 0)) { + keyname.erase(keyname.size() - 5, 5); + modifier = Key::Ctrl; + } else if ((keyname.size() > 4) && (keyname.substr(keyname.size() - 4, 4).compare("+alt") == 0)) { + keyname.erase(0, keyname.size() -46); + modifier = Key::Shift; + } + Key *key = find(name); if (key) { - key->clear(Key::None); + key->clear(modifier); } else { con_print << "Key '" << name << "' not found." << std::endl; } @@ -381,45 +449,24 @@ void Keyboard::list_keys() con_print << keys.size() << " registered keys" << std::endl; } -void Keyboard::list_bind(std::string const &name) -{ - Key *key = find(name); - if (key) { - if (key->bind(Key::None).size()) { - con_print << " " << aux::pad_left(key->name(), 6) << " " << key->bind(Key::None) << std::endl; - } - if (key->bind(Key::Shift).size()) { - con_print << " shift+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Shift) << std::endl; - } - if (key->bind(Key::Ctrl).size()) { - con_print << " ctrl+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Ctrl) << std::endl; - } - if (key->bind(Key::Alt).size()) { - con_print << " alt+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Alt) << std::endl; - } - } else { - con_print << "key '" << name << "' not found" << std::endl; - } -} - void Keyboard::list_binds() { size_t n =0; for (iterator it = begin(); it != end(); it++) { if ((*it).second->bind(Key::None).size()) { - con_print << " " << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::None) << std::endl; + con_print << " " << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::None) << std::endl; n++; } if ((*it).second->bind(Key::Shift).size()) { - con_print << " shift+" << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::Shift) << std::endl; + con_print << " shift+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Shift) << std::endl; n++; } if ((*it).second->bind(Key::Ctrl).size()) { - con_print << " ctrl+" << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::Ctrl) << std::endl; + con_print << " ctrl+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Ctrl) << std::endl; n++; } if ((*it).second->bind(Key::Alt).size()) { - con_print << " alt+" << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::Alt) << std::endl; + con_print << " alt+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Alt) << std::endl; n++; } diff --git a/src/client/keyboard.h b/src/client/keyboard.h index e4995e6..2841495 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -30,7 +30,7 @@ public: /// find a key on name Key *find(std::string const & name); - /// bind a string to a key + /// bind a string to a key, if str is empty, just list void bind(std::string const &name, const std::string str); /// clear the string bound to a key @@ -48,9 +48,6 @@ public: /// list keyboard binds void list_binds(); - /// list binds for a singe key - void list_bind(std::string const &name); - /// load keyboard binds void load_binds(); -- cgit v1.2.3