4

I want to remap the keys on my keyboard. I want to do it programatically, because I will use some logic to decide what to input. For example, when a user press "A" followed by "S", I will output "λ". In other words, everytime I type a key on my keyboard, I want to intercept it, apply some logic to it and give it back changed before it goes to the focused app.

How?

2

Have you looked into Keyboard Maestro? I use it all the time, I believe you can accomplish with what you want using Keyboard Maestro.

For example: You can have it check for a typed in string "good" and return morning or afternoon based on the time of day.

enter image description here

With Keyboard Maestro you can execute AppleScript or Shell scripts. Keyboard Maestro is pretty powerful. Your macros can then be global or specific to an application.

2

Most of the text macro tools available on the Mac will be able to trigger scripts (Applescript, shell scripts, etc.) as well as perform standard expansions based on the input and context.

I like to start my expansions with the semi-colon since I know I will rarely accidentally trigger an expansion in my normal typing, and it's an easy key to hit.

For instance:

  • When I type ";check", it automatically expands to "✓"
  • ;yen → ¥
  • ;dt → 2014-07-11 (current date)
  • ;email → expands to my email address
  • etc.

These expansions can also call scripts rather than basic substitutions to perform more advanced functions if you'd like. Some popular software options for this are aText, Typinator and TextExpander among many others.

1

The answers that others have given are probably the way you want to do this, but if you really wanted to intercept and inject keystrokes, check out how Karabiner (formerly KeyRemap4MacBook) does it.

Here's the interesting bit: https://github.com/tekezo/Karabiner/tree/master/src/core/kext

It would be pretty involved to adapt their code and do what you want, but it's certainly possible.

1

Another option is to create ~/Library/KeyBindings/, save

{
  "a" = {
    "s" = (insertText:, "λ");
  };
}

as ~/Library/KeyBindings/DefaultKeyBinding.dict, and quit and reopen applications to apply changes.

That prevents you from inserting a, but you might replace a with another key combination like ~a (option-a) or \Uf704 (F1).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .