diff --git a/features/run_control/key.feature b/features/run_control/key.feature index 842f1c8..1c9533c 100644 --- a/features/run_control/key.feature +++ b/features/run_control/key.feature @@ -8,3 +8,12 @@ Feature: `key' run control keyword And uhwm is running When I press the alt+f keys Then the output must contain "trigger f key code" + + Scenario: translates common key names to their X equivalent + Given a run control file with: + """ + key(:enter) { puts 'trigger return key code' } + """ + And uhwm is running + When I press the alt+Return keys + Then the output must contain "trigger return key code" diff --git a/lib/uh/wm/run_control.rb b/lib/uh/wm/run_control.rb index fc66ceb..79e8a00 100644 --- a/lib/uh/wm/run_control.rb +++ b/lib/uh/wm/run_control.rb @@ -1,6 +1,13 @@ module Uh module WM class RunControl + KEYSYM_TRANSLATIONS = { + backspace: :BackSpace, + enter: :Return, + return: :Return, + tab: :Tab + }.freeze + class << self def evaluate env rc_path = File.expand_path(env.rc_path) @@ -18,7 +25,14 @@ module Uh end def key keysym, &block - @env.keybinds[keysym] = block + @env.keybinds[translate_keysym keysym] = block + end + + + private + + def translate_keysym keysym + KEYSYM_TRANSLATIONS.key?(keysym) ? KEYSYM_TRANSLATIONS[keysym] : keysym end end end diff --git a/spec/uh/wm/run_control_spec.rb b/spec/uh/wm/run_control_spec.rb index 3ac1f39..43498e5 100644 --- a/spec/uh/wm/run_control_spec.rb +++ b/spec/uh/wm/run_control_spec.rb @@ -52,15 +52,20 @@ module Uh describe '#key' do let(:code) { -> { :keybind_code } } - before { rc.key :f, &code } - it 'registers a key binding in the env' do + rc.key :f, &code expect(env.keybinds.keys).to include :f end it 'registers given block with the key binding' do + rc.key :f, &code expect(env.keybinds[:f].call).to eq :keybind_code end + + it 'translates common key names to equivalent X keysym' do + rc.key :enter, &code + expect(env.keybinds.keys).to include :Return + end end end end