Keymap: Add ability to remove a key mapping

... of a normal (non-modifier) key via a right click menu.
This commit is contained in:
John Scipione
2015-01-07 21:11:39 -05:00
parent 6aa4d138bf
commit b433c2ad87
4 changed files with 133 additions and 107 deletions
+115 -105
View File
@@ -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
+1 -1
View File
@@ -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;
+2 -1
View File
@@ -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 {
+15
View File
@@ -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;