From b433c2ad87ce85265ca5674be338f3716372a512 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Wed, 7 Jan 2015 21:11:39 -0500 Subject: [PATCH] Keymap: Add ability to remove a key mapping ... of a normal (non-modifier) key via a right click menu. --- src/preferences/keymap/KeyboardLayoutView.cpp | 220 +++++++++--------- src/preferences/keymap/Keymap.cpp | 2 +- src/preferences/keymap/KeymapApplication.h | 3 +- src/preferences/keymap/KeymapWindow.cpp | 15 ++ 4 files changed, 133 insertions(+), 107 deletions(-) diff --git a/src/preferences/keymap/KeyboardLayoutView.cpp b/src/preferences/keymap/KeyboardLayoutView.cpp index 7586bcf25d..0eec89afb6 100644 --- a/src/preferences/keymap/KeyboardLayoutView.cpp +++ b/src/preferences/keymap/KeyboardLayoutView.cpp @@ -215,112 +215,122 @@ KeyboardLayoutView::MouseDown(BPoint point) && (modifiers() & B_CONTROL_KEY) != 0)) { // secondary mouse button, pop up a swap context menu if (!is_mappable_to_modifier(key->code)) { - // ToDo: pop up a list of alternative characters - fButtons = buttons; - return; + // ToDo: Pop up a list of alternative characters to map + // the key to. Currently we only add an option to remove the + // current key mapping. + BPopUpMenu* alternativesPopUp = new BPopUpMenu( + "Alternatives pop up", true, true, B_ITEMS_IN_COLUMN); + BMessage* message = new BMessage(kMsgUpdateNormalKeys); + message->AddUInt32("keyCode", key->code); + message->AddBool("unset", true); + alternativesPopUp->AddItem(new BMenuItem(B_TRANSLATE("Remove"), + message)); + alternativesPopUp->SetAsyncAutoDestruct(true); + if (alternativesPopUp->SetTargetForItems(Window()) == B_OK) + alternativesPopUp->Go(ConvertToScreen(point), true); + } else { + // pop up the modifier keys menu + BPopUpMenu* modifiersPopUp = new BPopUpMenu("Modifiers pop up", + true, true, B_ITEMS_IN_COLUMN); + const key_map& map = fKeymap->Map(); + bool isLockKey = is_lock_key(key->code); + BMenuItem* item = NULL; + + if (is_left_modifier_key(key->code) || isLockKey) { + item = _CreateSwapModifiersMenuItem(B_LEFT_SHIFT_KEY, + isLockKey ? B_LEFT_SHIFT_KEY : B_SHIFT_KEY, + map.left_shift_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.left_shift_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_LEFT_CONTROL_KEY, + isLockKey ? B_LEFT_CONTROL_KEY : B_CONTROL_KEY, + map.left_control_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.left_control_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_LEFT_OPTION_KEY, + isLockKey ? B_LEFT_OPTION_KEY : B_OPTION_KEY, + map.left_option_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.left_option_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_LEFT_COMMAND_KEY, + isLockKey ? B_LEFT_COMMAND_KEY : B_COMMAND_KEY, + map.left_command_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.left_command_key) + item->SetMarked(true); + } + + if (is_right_modifier_key(key->code) || isLockKey) { + if (isLockKey) + modifiersPopUp->AddSeparatorItem(); + + item = _CreateSwapModifiersMenuItem(B_RIGHT_SHIFT_KEY, + isLockKey ? B_RIGHT_SHIFT_KEY : B_SHIFT_KEY, + map.right_shift_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.right_shift_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_RIGHT_CONTROL_KEY, + isLockKey ? B_RIGHT_CONTROL_KEY : B_CONTROL_KEY, + map.right_control_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.right_control_key) + item->SetMarked(true); + } + + item = _CreateSwapModifiersMenuItem(B_MENU_KEY, B_MENU_KEY, + map.menu_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.menu_key) + item->SetMarked(true); + + if (is_right_modifier_key(key->code) || isLockKey) { + item = _CreateSwapModifiersMenuItem(B_RIGHT_OPTION_KEY, + isLockKey ? B_RIGHT_OPTION_KEY : B_OPTION_KEY, + map.right_option_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.right_option_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_RIGHT_COMMAND_KEY, + isLockKey ? B_RIGHT_COMMAND_KEY : B_COMMAND_KEY, + map.right_command_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.right_command_key) + item->SetMarked(true); + } + + modifiersPopUp->AddSeparatorItem(); + + item = _CreateSwapModifiersMenuItem(B_CAPS_LOCK, B_CAPS_LOCK, + map.caps_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.caps_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_NUM_LOCK, B_NUM_LOCK, + map.num_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.num_key) + item->SetMarked(true); + + item = _CreateSwapModifiersMenuItem(B_SCROLL_LOCK, B_SCROLL_LOCK, + map.scroll_key, key->code); + modifiersPopUp->AddItem(item); + if (key->code == map.scroll_key) + item->SetMarked(true); + + modifiersPopUp->SetAsyncAutoDestruct(true); + if (modifiersPopUp->SetTargetForItems(Window()) == B_OK) + modifiersPopUp->Go(ConvertToScreen(point), true); } - - // pop up the modifier keys menu - BPopUpMenu* modifiersPopUp = new BPopUpMenu("Modifiers pop up", - true, true, B_ITEMS_IN_COLUMN); - const key_map& map = fKeymap->Map(); - bool isLockKey = is_lock_key(key->code); - BMenuItem* item = NULL; - - if (is_left_modifier_key(key->code) || isLockKey) { - item = _CreateSwapModifiersMenuItem(B_LEFT_SHIFT_KEY, - isLockKey ? B_LEFT_SHIFT_KEY : B_SHIFT_KEY, - map.left_shift_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.left_shift_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_LEFT_CONTROL_KEY, - isLockKey ? B_LEFT_CONTROL_KEY : B_CONTROL_KEY, - map.left_control_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.left_control_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_LEFT_OPTION_KEY, - isLockKey ? B_LEFT_OPTION_KEY : B_OPTION_KEY, - map.left_option_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.left_option_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_LEFT_COMMAND_KEY, - isLockKey ? B_LEFT_COMMAND_KEY : B_COMMAND_KEY, - map.left_command_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.left_command_key) - item->SetMarked(true); - } - - if (is_right_modifier_key(key->code) || isLockKey) { - if (isLockKey) - modifiersPopUp->AddSeparatorItem(); - - item = _CreateSwapModifiersMenuItem(B_RIGHT_SHIFT_KEY, - isLockKey ? B_RIGHT_SHIFT_KEY : B_SHIFT_KEY, - map.right_shift_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.right_shift_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_RIGHT_CONTROL_KEY, - isLockKey ? B_RIGHT_CONTROL_KEY : B_CONTROL_KEY, - map.right_control_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.right_control_key) - item->SetMarked(true); - } - - item = _CreateSwapModifiersMenuItem(B_MENU_KEY, B_MENU_KEY, - map.menu_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.menu_key) - item->SetMarked(true); - - if (is_right_modifier_key(key->code) || isLockKey) { - item = _CreateSwapModifiersMenuItem(B_RIGHT_OPTION_KEY, - isLockKey ? B_RIGHT_OPTION_KEY : B_OPTION_KEY, - map.right_option_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.right_option_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_RIGHT_COMMAND_KEY, - isLockKey ? B_RIGHT_COMMAND_KEY : B_COMMAND_KEY, - map.right_command_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.right_command_key) - item->SetMarked(true); - } - - modifiersPopUp->AddSeparatorItem(); - - item = _CreateSwapModifiersMenuItem(B_CAPS_LOCK, B_CAPS_LOCK, - map.caps_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.caps_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_NUM_LOCK, B_NUM_LOCK, - map.num_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.num_key) - item->SetMarked(true); - - item = _CreateSwapModifiersMenuItem(B_SCROLL_LOCK, B_SCROLL_LOCK, - map.scroll_key, key->code); - modifiersPopUp->AddItem(item); - if (key->code == map.scroll_key) - item->SetMarked(true); - - modifiersPopUp->SetAsyncAutoDestruct(true); - if (modifiersPopUp->SetTargetForItems(Window()) == B_OK) - modifiersPopUp->Go(ConvertToScreen(point), true); } else if ((buttons & B_TERTIARY_MOUSE_BUTTON) != 0 && (fButtons & B_TERTIARY_MOUSE_BUTTON) == 0) { // tertiary mouse button, toggle the "deadness" of dead keys diff --git a/src/preferences/keymap/Keymap.cpp b/src/preferences/keymap/Keymap.cpp index 1234e8fd94..7dda76d6da 100644 --- a/src/preferences/keymap/Keymap.cpp +++ b/src/preferences/keymap/Keymap.cpp @@ -374,7 +374,7 @@ Keymap::SetKey(uint32 keyCode, uint32 modifiers, int8 deadKey, if (offset < 0) return; - if (numBytes == -1) + if (numBytes < 0) numBytes = strlen(bytes); if (numBytes > 6) return; diff --git a/src/preferences/keymap/KeymapApplication.h b/src/preferences/keymap/KeymapApplication.h index 1802dcb7e4..2f73ca2cab 100644 --- a/src/preferences/keymap/KeymapApplication.h +++ b/src/preferences/keymap/KeymapApplication.h @@ -23,7 +23,8 @@ static const uint32 kMsgShowModifierKeysWindow = 'smkw'; static const uint32 kMsgCloseModifierKeysWindow = 'hmkw'; -static const uint32 kMsgUpdateModifierKeys = 'umks'; +static const uint32 kMsgUpdateModifierKeys = 'umod'; +static const uint32 kMsgUpdateNormalKeys = 'ukey'; class KeymapApplication : public BApplication { diff --git a/src/preferences/keymap/KeymapWindow.cpp b/src/preferences/keymap/KeymapWindow.cpp index 4b8b9acc3f..0c90022649 100644 --- a/src/preferences/keymap/KeymapWindow.cpp +++ b/src/preferences/keymap/KeymapWindow.cpp @@ -320,6 +320,21 @@ KeymapWindow::MessageReceived(BMessage* message) _UpdateButtons(); break; + case kMsgUpdateNormalKeys: + { + uint32 keyCode; + if (message->FindUInt32("keyCode", &keyCode) != B_OK) + break; + + bool unset; + if (message->FindBool("unset", &unset) == B_OK && unset) { + fCurrentMap.SetKey(keyCode, modifiers(), 0, "", 0); + _UpdateButtons(); + fKeyboardLayoutView->SetKeymap(&fCurrentMap); + } + break; + } + case kMsgUpdateModifierKeys: { uint32 keyCode;