Skip to content

Instantly share code, notes, and snippets.

@axsann
Created December 7, 2022 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axsann/35e53b14d4962ba789d7346bb47b5227 to your computer and use it in GitHub Desktop.
Save axsann/35e53b14d4962ba789d7346bb47b5227 to your computer and use it in GitHub Desktop.
enthumble-mac (Macでenthumble風のキーボード設定)
-- hammerspoon用の設定
local function keyCode(key, modifiers)
modifiers = modifiers or {}
return function()
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), true):post()
hs.timer.usleep(1000)
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), false):post()
end
end
local function remapKey(modifiers, key, keyCode)
hs.hotkey.bind(modifiers, key, keyCode, nil, keyCode)
end
-- カーソル移動
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'l', keyCode('right'))
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'h', keyCode('left'))
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'j', keyCode('left'))
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'k', keyCode('down'))
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'i', keyCode('up'))
-- 削除
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'u', keyCode('delete'))
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'o', keyCode('forwarddelete'))
-- Enter
remapKey({'alt', 'ctrl', 'shift', 'cmd'}, 'space', keyCode('return'))
-- テキスト編集
-- remapKey({'ctrl'}, 'w', keyCode('x', {'cmd'}))
-- remapKey({'ctrl'}, 'y', keyCode('v', {'cmd'}))
-- コマンド
-- remapKey({'ctrl'}, 's', keyCode('f', {'cmd'}))
-- remapKey({'ctrl'}, '/', keyCode('z', {'cmd'}))
-- remapKey({'ctrl'}, 'g', keyCode('escape'))
-- ページスクロール
-- remapKey({'ctrl', 'shift'}, 'k', keyCode('pagedown'))
-- remapKey({'ctrl', 'shift'}, 'i', keyCode('pageup'))
-- remapKey({'ctrl', 'shift'}, 'j', keyCode('home'))
-- remapKey({'ctrl', 'shift'}, 'l', keyCode('end'))
--- Perfect backslash(\) for Mac JIS keybord users
-- The problem:
-- On Mac, Japanese IME setting to replace Yen-backslash is ignored by
-- IntelliJ, jEdit or such as because JVM uses another keymap traditionally.
-- Solution:
-- Use Hammerspoon (http://www.hammerspoon.org/) instead of Japanese IME
-- setting. Paste below to your ~/.hammerspoon/init.lua file.
-- @author Hisateru Tanaka (tanakahisateru@gmail.com)
local VK_1 = 0x12
local VK_ESC = 0x35
-- Secret key codes not included in hs.keycodes.map
local VK_JIS_YEN = 0x5d
local VK_JIS_UNDERSCORE = 0x5e
--local log = hs.logger.new('keyhook','debug')
function flagsMatches(flags, modifiers)
local set = {}
for _, k in ipairs(modifiers) do set[string.lower(k)] = true end
for _, k in ipairs({'fn', 'cmd', 'ctrl', 'alt', 'shift'}) do
if set[k] ~= flags[k] then return false end
end
return true
end
-- NEVER define as local variable!
jisKeyboardFilter = hs.eventtap.new({
hs.eventtap.event.types.keyDown,
hs.eventtap.event.types.keyUp
}, function(event)
local c = event:getKeyCode()
local f = event:getFlags()
-- log.d(...)
if c == VK_JIS_YEN then
-- To input \ even if JVM, toggle Option key status when Yen key.
if flagsMatches(f, {'alt'}) then
event:setFlags({})
elseif flagsMatches(f, {}) then
event:setFlags({alt=true})
end
-- Hint: Never replace key code to backslash itself because JIS
-- keyboard does'nt have phisical backslash and assignes it to close
-- bracket (]) key.
elseif c == VK_JIS_UNDERSCORE then
-- Also map single undetscore (_) key to backslash (\).
if flagsMatches(f, {}) then
event:setKeyCode(VK_JIS_YEN)
event:setFlags({alt=true})
end
end
end)
jisKeyboardFilter:start()
{
"global": {
"check_for_updates_on_startup": true,
"show_in_menu_bar": true,
"show_profile_name_in_menu_bar": false
},
"profiles": [
{
"complex_modifications": {
"parameters": {
"basic.simultaneous_threshold_milliseconds": 50,
"basic.to_delayed_action_delay_milliseconds": 500,
"basic.to_if_alone_timeout_milliseconds": 1000,
"basic.to_if_held_down_threshold_milliseconds": 500,
"mouse_motion_to_scroll.speed": 100
},
"rules": [
{
"description": "Change Eisuu to right_shift & right_option & right_control & right_command. (Post Eisuu if pressed alone)",
"manipulators": [
{
"conditions": [
{
"bundle_identifiers": [
"^com\\.parallels\\.desktop"
],
"type": "frontmost_application_unless"
}
],
"from": {
"key_code": "japanese_eisuu",
"modifiers": {
"optional": [
"any"
]
}
},
"to": [
{
"key_code": "right_shift",
"modifiers": [
"right_option",
"right_control",
"right_command"
]
}
],
"to_if_alone": [
{
"key_code": "japanese_eisuu"
}
],
"type": "basic"
}
]
}
]
},
"devices": [
{
"disable_built_in_keyboard_if_exists": false,
"fn_function_keys": [],
"identifiers": {
"is_keyboard": false,
"is_pointing_device": true,
"product_id": 1957,
"vendor_id": 1118
},
"ignore": false,
"manipulate_caps_lock_led": false,
"simple_modifications": []
},
{
"disable_built_in_keyboard_if_exists": false,
"fn_function_keys": [],
"identifiers": {
"is_keyboard": true,
"is_pointing_device": false,
"product_id": 615,
"vendor_id": 76
},
"ignore": false,
"manipulate_caps_lock_led": true,
"simple_modifications": []
}
],
"fn_function_keys": [
{
"from": {
"key_code": "f1"
},
"to": [
{
"consumer_key_code": "display_brightness_decrement"
}
]
},
{
"from": {
"key_code": "f2"
},
"to": [
{
"consumer_key_code": "display_brightness_increment"
}
]
},
{
"from": {
"key_code": "f3"
},
"to": [
{
"key_code": "mission_control"
}
]
},
{
"from": {
"key_code": "f4"
},
"to": [
{
"key_code": "launchpad"
}
]
},
{
"from": {
"key_code": "f5"
},
"to": [
{
"key_code": "illumination_decrement"
}
]
},
{
"from": {
"key_code": "f6"
},
"to": [
{
"key_code": "illumination_increment"
}
]
},
{
"from": {
"key_code": "f7"
},
"to": [
{
"consumer_key_code": "rewind"
}
]
},
{
"from": {
"key_code": "f8"
},
"to": [
{
"consumer_key_code": "play_or_pause"
}
]
},
{
"from": {
"key_code": "f9"
},
"to": [
{
"consumer_key_code": "fastforward"
}
]
},
{
"from": {
"key_code": "f10"
},
"to": [
{
"consumer_key_code": "mute"
}
]
},
{
"from": {
"key_code": "f11"
},
"to": [
{
"consumer_key_code": "volume_decrement"
}
]
},
{
"from": {
"key_code": "f12"
},
"to": [
{
"consumer_key_code": "volume_increment"
}
]
}
],
"name": "Default profile",
"parameters": {
"delay_milliseconds_before_open_device": 1000
},
"selected": true,
"simple_modifications": [],
"virtual_hid_keyboard": {
"caps_lock_delay_milliseconds": 0,
"country_code": 0,
"indicate_sticky_modifier_keys_state": true,
"keyboard_type": "jis",
"mouse_key_xy_scale": 100
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment