Remap keyboard?

I would like to create a simple app to capture/remap(?) space bar to underscore globally, even when app is in background, and be able to toggle this feature. EDIT: For MacOS.

Is this even possible with a Swift app? If so, what APIs or classes should I look into?

Thanks...

“globally” is a word that needs clarification. Is the purpose of your application to intercept and modify keyboard input system‐wide? Or are you wanting to interpret all input to your specific application in a modified way?

Yes, system-wide, so whatever app I'm using, this will have effect.

One more question. Are you wanting to write an application (i.e. to sell or for a school project or something), or do you just want to be able to use your computer that way?

Mainly just use my computer that way. But I also thought it would make a good first time Swift project, as I am just now learning to build apps in Xcode.

Well it is not likely a good first time Swift project. It requires a level of integration with the operating system that is highly unusual. It would certainly not result in an application that would be allowed on the App Store. (Think about the security vulnerabilities if the operating system let arbitrary applications silently intercept keyboard input destined for other applications. You would be able to listen to every password as it is typed in.)

However, if you play by the operating system’s rules and do things its way, it is easy enough to get your keyboard to work like that. macOS lets you create custom keyboard layouts (in addition to the hundreds that come preinstalled). Ukelele is an application from SIL that can help you generate custom layouts in a more user‐friendly manner. It cannot do everything the raw XML format can, but is much easier to use for simple changes like what you are suggesting.

You could simply create a layout where the space key produces an underscore. As for toggling it on and off, you can have two keyboards active and set a system hotkey to switch between them. Or you could reinterpret what Caps Lock should do, or only make Shift + Space be an underscore.

The sky is the limit—I can type “¤com” to get ⌘, “¤eu” to get €, or “¤sum” to get ∑ in my keyboard.

But unfortunately none of that helps you practice Swift, so you will have to think of something else for that.

I could use something like Karabiner, but it would be cumbersome to keep configuring it back and forth. I thought to create an app where I could easily toggle between the mapped combination, and the default.

On the other hand, I did write my own Swift‐based keyboard generator way back in Swift 2 and have been meaning to update and open source it. I could see putting it on GitHub as‐is and you could get some practice in Swift by helping me modernize it for Swift 5 and generalize it for a broader set of use cases. Is that something you would be interested in?

I would be interested in seeing it on github just to see what I can personally glean from it, but while it is possible I might contribute a thing or two, I wouldn't want to lead you on that I would be committed to contributing to a broader project. I'm kind of a lone wolf these days, and just like to play with stuff for my own and friends' use :slight_smile:

Regarding "first app" projects, I do tend to jump into the deeper end of the pool when I'm choosing a project to learn on. When I choose a project, it has to be something that's actually meaningful to me personally, and not just an exercise. And if I create my own app, even one to get a functionality that is available in other apps, I can make it custom to my own workflow.

BTW, thanks for the tips regarding the layouts, sounds like very useful information.

If you just want a program that lets you remap keys, have a look at either Karabiner or BetterTouchTool. Karabiner is open source with the code available on GitHub here. It’s mostly C++, but I’m sure you can figure out how to translate the languages :-)

For myself, I have a custom keyboard layout that I made with Ukelele, I have Karabiner configured with a couple mods to the caps lock and escape keys, and I have a huge number of gestures and shortcuts configured in BTT.

(I also tend to customize menu-item shortcuts through System Preferences.)