From 30364b7290b3f33bfa55ef83c5ecf029d9e136cb Mon Sep 17 00:00:00 2001 From: crondog Date: Sat, 12 Jul 2014 22:29:21 +1000 Subject: [PATCH] Fix key modifiers ps2: Use Mod vars and clean up mac overrides ps3: Fix var names --- lib/keyboard_shortcuts.js | 109 ++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/lib/keyboard_shortcuts.js b/lib/keyboard_shortcuts.js index e8fbcd1..3048452 100644 --- a/lib/keyboard_shortcuts.js +++ b/lib/keyboard_shortcuts.js @@ -90,96 +90,93 @@ var keyboard_shortcuts = function(spec, my) { // @event {object} keyboard event // ``` handler = function(evt) { - var modifier = (1 << 1); /* ctrl */ - var modifier_key = 17; - if(process.platform === 'darwin') { - modifier = (1 << 3); /* command */ - modifier_key = 91; - } - /* Use for ctrl shortcut on darwin. */ - var ctrl = (1 << 1); /* ctrl */ - var ctrl_key = 17; + common.log.out(JSON.stringify(evt)); + + var ControlKey = 17; - //common.log.out(JSON.stringify(evt)); + var ShiftMod = (1 << 0); + var ControlMod = (1 << 1); + var AltMod = (1 << 2); + var MetaMod = (1 << 3); - if(evt.type === 7 && (evt.modifiers === modifier) && - evt.keycode === 84) { - /* Ctrl - T ; Repetition OK */ - that.emit('new'); + if(process.platform == 'darwin'){ + ControlMod = MetaMod; + ControlKey = 91; } - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && + + var ShiftControlMod = ShiftMod | ControlMod; + + if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && evt.keycode === 84 && !is_last(evt)) { /* Ctrl - Shift - T ; No Repetition */ that.emit('recover'); } - - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && + evt.keycode === 84) { + /* Ctrl - T ; Repetition OK */ + that.emit('new'); + } + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && (evt.keycode === 76 || evt.keycode === 32) && !is_last(evt)) { /* Ctrl - L | Space ; No Repetition */ that.emit('go'); } - - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && + evt.keycode === 74 && !is_last(evt)) { + /* Ctrl - Shift - J ; No Repetition */ + that.emit('back'); + } + else if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && + evt.keycode === 75 && !is_last(evt)) { + /* Ctrl - Shift - K ; No Repetition */ + that.emit('forward'); + } + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && (evt.keycode === 74 || evt.keycode === 40)) { /* Ctrl - J | Down ; Repetition OK */ that.emit('next'); my.can_commit = true; } - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && (evt.keycode === 75 || evt.keycode === 38)) { /* Ctrl - K | Up ; Repetition OK */ that.emit('prev'); my.can_commit = true; } - if(evt.type === 9 && (evt.modifiers === ctrl) && + else if(evt.type === 9 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && (evt.keycode === 9)) { - /* Ctrl - Tab ; Repetition OK */ - that.emit('next'); + /* Ctrl - Shift - Tab ; Repetition OK */ + that.emit('prev'); my.can_commit = true; } - if(evt.type === 7 && (evt.modifiers === (1 << 0 | ctrl)) && + else if(evt.type === 9 && ((evt.modifiers & ControlMod) === ControlMod) && (evt.keycode === 9)) { - /* Ctrl - Shift - Tab ; Repetition OK */ - that.emit('prev'); + /* Ctrl - Tab ; Repetition OK */ + that.emit('next'); my.can_commit = true; } - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && (evt.keycode >= 49 && evt.keycode <= 57)) { /* Ctrl - 1-9 ; Repetiton OK */ that.emit('select_by_index', evt.keycode - 49); my.can_commit = true; } - - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && - evt.keycode === 74 && !is_last(evt)) { - /* Ctrl - Shift - J ; No Repetition */ - that.emit('back'); - } - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && - evt.keycode === 75 && !is_last(evt)) { - /* Ctrl - Shift - K ; No Repetition */ - that.emit('forward'); - } - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 37 && !is_last(evt)) { /* Ctrl - Left ; No Repetition */ that.emit('back'); } - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 39 && !is_last(evt)) { /* Ctrl - Right ; No Repetition */ that.emit('forward'); } - - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && + else if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && evt.keycode === 72 && !is_last(evt)) { /* Ctrl - Shift - H ; No Repetition */ that.emit('toggle'); } - - if(evt.type === 9 && - (evt.keycode === modifier_key || - evt.keycode === ctrl_key)) { + else if(evt.type === 9 && evt.keycode === ControlKey) { /* Ctrl (Release); No Repetition */ if(my.can_commit) { my.can_commit = false; @@ -187,7 +184,7 @@ var keyboard_shortcuts = function(spec, my) { } } /* CapsLock as a Ctrl case */ - if(evt.type === 9 && (evt.modifiers === modifier) && + else if(evt.type === 9 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 20) { /* Ctrl (Release); No Repetition */ if(my.can_commit) { @@ -195,44 +192,40 @@ var keyboard_shortcuts = function(spec, my) { that.emit('commit'); } } - - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 87) { /* Ctrl - W ; Repetition OK */ that.emit('close'); } - - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 80 && !is_last(evt)) { /* Ctrl - W ; No Repetition */ that.emit('stack_pin'); } - - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 70 && !is_last(evt)) { /* Ctrl - F ; No Repetition */ that.emit('find_in_page'); } - if(evt.type === 7 && (evt.modifiers === modifier) && + else if(evt.type === 7 && ((evt.modifiers & ControlMod) === ControlMod) && evt.keycode === 82 && !is_last(evt)) { /* Ctrl - R ; No Repetition */ that.emit('reload'); } - - if(evt.type === 7 && evt.keycode === 27) { + else if(evt.type === 7 && evt.keycode === 27) { /* ESC ; Repetition OK */ that.emit('clear'); } if(process.platform === 'darwin') { - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && + if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && evt.keycode === 221) { /* Ctrl - } ; Repetition OK */ that.emit('next'); my.can_commit = true; } - if(evt.type === 7 && (evt.modifiers === (1 << 0 | modifier)) && + else if(evt.type === 7 && ((evt.modifiers & ShiftControlMod) === ShiftControlMod) && evt.keycode === 219) { /* Ctrl - { ; Repetition OK */ that.emit('prev');