Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I define and execute my own shell scripts in Karabiner Elements? #721

Closed
zanglebert opened this issue Apr 28, 2017 · 23 comments
Closed
Labels

Comments

@zanglebert
Copy link

Hi

I don't know what I should expect to work if I'd go from Karabiner (currently working, didn't upgrade to Sierra) to Karabiner Elements. The readme doesn't seem to mention it, or I didn't know where to look -- apologies if it's the latter.

I'm using Karabiner for one things mainly: executing shell commands and AppleScript commands, bound to global or app specific shortcuts, i.e. using commands I defined in private.xml.

Is the current version of Elements able to do this, or is this one of the parts not implemented yet?

@carlsmith
Copy link
Contributor

carlsmith commented Apr 28, 2017

Elements is currently not able to do that. It basically just maps keys to key functions, as in the things a key can do, including stuff like illumination_decrement and play_or_pause.

If you're willing to edit the XML file directly, you can access functionality not yet available in the GUI, but I'm not sure if you can assign keys to scripts like that yet.

There's some support for binding shortcuts to scripts built into macOS. Check out this SO post.

This is pretty much every option Elements currently offers:

  • Simple Modifications: Map normal keys to arbitrary key functions
  • Function Keys: Map function keys (F1-12) to arbitrary key functions
  • Devices: Apply modifications to specific keyboards only (not currently working correctly)
  • Virtual Keyboard: Set the virtual keyboard's type (ANSI, ISO, JIS) and its Caps Lock Delay
  • Profiles: Create multiple profiles that you can switch between
  • Misc: Enable automatic updates, delete Karabiner Elements etc.
  • Log: View the log

Karabiner Elements still supports inverting the function keys, where F1 is just F1, and you hold fn and hit F1 for display_brightness-decrement.

The Event Viewer from the old version is also available.

@zanglebert
Copy link
Author

Thanks a lot for this (high-level) summary.

Any chance this would be a good addition to the readme, in case others are wondering about this as well?

@carlsmith
Copy link
Contributor

carlsmith commented Apr 29, 2017

Thanks. I'm only a user, so have no control over what goes in the README, but I did write this in the hope it would answer your question and offer a quick status report for anyone else that comes here.

@quantumgolem
Copy link

@carlsmith Perhaps you can make a pull request

@carlsmith
Copy link
Contributor

Done (#724). Thanks guys.

@carrigmore
Copy link

@carlsmith
in old Karabiner I can map to a windows num lock key, in private.xml
KeyToKey KeyCode::KEYPAD_CLEAR, KeyCode::VK_IOHIKEYBOARD_TOGGLE_NUMLOCK
Do you know if it is possible to achieve same in the elements? Thanks.

@carlsmith
Copy link
Contributor

carlsmith commented May 1, 2017

@carrigmore - Yep. You can. It's easy to do just using the GUI.

Under the Simple Modifications tab, if you hit the Add item button, you get two drop down menus, From key and To key. In those, there's a section named Keypad keys, and keypad_num_lock is the first item. Just select that from the From key menu, and select whatever you want it mapped to from To key.

I tested just mapping it to x, and it worked fine. It's pretty straight forwards, but let me know if you have any trouble.

numlock_mapping

@carrigmore
Copy link

carrigmore commented May 2, 2017

screen shot 2017-04-30 at 16 31 57
@carlsmith Thanks for your reply. the from key is right, I find that as well, the 'from key' is 'key_pad_numlock (that is the mac clear key)' but the problem is the 'to key' (windows num lock key) I want to map to doesn't appear in the 'To key' column. The to_key is windows num lock key. It doesn't seem to exist in the 'To key' column. In previous version I can do this by editing the private.xml file, and after correct mapping the LED of num_lock on my TK quick fire keyboard will light on after press the clear key (as it has been mapped to windows num lock key correctly) and the numbers pad can be used to input numbers.

Currently in element It can only map to a 'pc_sroll_lock' but not 'num_lock', you see the screenshot, the pc number lock key is not in the list.

@carrigmore
Copy link

@carlsmith I find user can edit the JSON file, but for some reason cannot map a key to windows locking_num_lock. As it has "not_to" : true

{
"name": "locking_num_lock",
"not_to": true
},

@carlsmith
Copy link
Contributor

I'm on my phone, but as far as I know, the Clear key on a Mac literally is the Numlock key. Keyboards don't know what their keys do. They just report that the key at the top left of the numpad was pressed, and the OS, based on layout software, determines what that key does, so there's no distinction between Clear and Numlock at the keyboard or driver (Karabiner) level. The legends on the keys are only a visual add that must assume a certain layout has been chosen in software. That's my understanding, but I may have totally misunderstood the issue. I'm new to this stuff.

@carlsmith
Copy link
Contributor

Sorry. Just read your message again. Ignore what I just wrote. I didn't even know it was possible to toggle Numlock state on macOS, so will have to just hope someone who knows better can help you out. Presumably, the missing Numlock key in the PC submenu was just an oversight that can be added without much trouble.

@carlsmith
Copy link
Contributor

Might be worth opening a new issue.

@carrigmore
Copy link

@carlsmith thanks a lot, I already created an issue.

@carrigmore
Copy link

@carlsmith in the Karabiner we can achieve this. The NumLock LED on keyboard will light up and the number pad keys can be used. This is quite useful when using a compact keyboard where number pad is used for multiple purposes (like TK quickfire, the number pad has other functions, if not toggled correctly cannot be used to input numbers)

@carlsmith
Copy link
Contributor

carlsmith commented May 3, 2017

The TK Quickfire looks really cool. I'd not seen a keyboard with that layout before. My main driver's an ANSI MINILA, so I get that some of the features most people never use can be important on smaller keyboards.

It's interesting that macOS supports Numlock. There's a popular ruby script on GitHub Gist somewhere that toggles the Caps Lock and Scroll Lock LEDs on the MINILA, well, on any keyboard. It'd be nice if I could get the Scroll Lock LED to indicate some other, actually useful state.

@HRG1
Copy link

HRG1 commented Jul 14, 2017

Shell commands are back in version 0.91.7. For example use "shell_command": "open '/Applications/Safari.app'" in the "to" section of the intended key combination (replacing "key_code: "x").

@smlewis
Copy link
Contributor

smlewis commented Jul 14, 2017

@HRG1 That's the feature I've been waiting for! This is glorious!

There seem to be few examples of user-edited karabiner.json for complex rules. Are they being collected as documentation? I've set mine up to have certain key commands launch new windows of certain applications (distinct from launching the application). It's entirely possible I just looked in the wrong places.

@jerguslejko
Copy link

@smlewis Could you share your solution? I am trying to map <esc> key to a shell command.

@smlewis
Copy link
Contributor

smlewis commented Aug 10, 2017

@jerguslejko I uploaded my JSON to this repository and the other one (that lets you pull complex configs down from the GUI).

https://github.com/pqrs-org/KE-complex_modifications/blob/master/docs/json/launch_new_Chrome_or_iTerm2_windows.json

https://github.com/tekezo/Karabiner-Elements/blob/master/examples/launch_Chrome_and_iTerm2_new_windows_keybindings.json

That JSON should get you started on your needs! You should be able to see how to add in your complex_modification manually. I guess you can edit it from scratch, or pull the one I put in KE-complex_modifications and copy and edit that.

@jerguslejko
Copy link

jerguslejko commented Aug 11, 2017

Thank you @smlewis! I managed to get it working.

For anyone wondering, I was trying to remap my escape key to play the "bell" sound every time I hit it and at the same time, make caps_lock function as escape (preparations for the new macbook with touch bar).

Here is my `~/.config/karabiner/karabiner.json` config
{
    "global": {
        "check_for_updates_on_startup": true,
        "show_in_menu_bar": true,
        "show_profile_name_in_menu_bar": false
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                    "basic.to_if_alone_timeout_milliseconds": 1000
                },
                "rules": [
                    {
                        "description": "map caps_lock to escape",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "caps_lock"
                                },
                                "to": [
                                    {
                                        "key_code": "escape"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
                    {
                        "description": "play bell sound when pressing the escape key",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "escape"
                                },
                                "to": [
                                    {
                                        "shell_command": "osascript -e 'beep'"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    }
                ]
            },
            "devices": [],
            "fn_function_keys": {
                "f1": "display_brightness_decrement",
                "f10": "mute",
                "f11": "volume_decrement",
                "f12": "volume_increment",
                "f2": "display_brightness_increment",
                "f3": "mission_control",
                "f4": "launchpad",
                "f5": "illumination_decrement",
                "f6": "illumination_increment",
                "f7": "rewind",
                "f8": "play_or_pause",
                "f9": "fastforward"
            },
            "name": "Default profile",
            "selected": true,
            "simple_modifications": {},
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "keyboard_type": "ansi"
            }
        }
    ]
}

@danfrincu
Copy link

danfrincu commented Oct 5, 2017

I came here looking for how to execute scripts from Karabiner Elements shortcut in Sierra (like I was able to do with Karabiner in El Capitan) and having looked at the examples given by folks I wanted to add one which was of interest to me and may be to others.
So what I do is take the output of a script and put it into the paste buffer, and trigger that with a shortcut. Above I've seen it was called out that you can use "shell_command" instead of "key_code" so it works by doing the following following.

"shell_command": "osascript -e 'do shell script \"/usr/local/bin/rt | /usr/bin/pbcopy\"'"

@stale
Copy link

stale bot commented Mar 4, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 4, 2018
@stale stale bot closed this as completed Mar 11, 2018
@oddlots
Copy link

oddlots commented Apr 9, 2018

@danfrincu Thanks, you gave me the bread crumbs to solve my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants