How do you type “Space” only if another key follows?

I would like to experiment with a macro, but I don't know how to build it. So thanks in advance for any help.

I would like this result:

  • If “Space” is pressed and another key follows within 0.x s, then type “Space”.

  • If “Space” is pressed and no other key follows within 0.x s, then do not type “Space”. In short: type/do nothing.

It's obviously easy to trigger on typing a space, but it's after that where I don't see a path forward. You need an action that can react to any key press, and (as far as I know), no such actions exist. If you can restrict it to a certain key or small set of keys, it would be possible with something like this:

Download Macro(s): To Space or Not To Space.kmmacros (5.1 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.5
  • Keyboard Maestro v11.0.3

This macro launches any time you press the Space Bar, then starts a timer. It stops when you press the A, B, or C keys, then checks if the duration was under a half second. If so, it erases whatever character you just typed (you don't say if you want to keep that character or not), then inserts a Space. If it's over a half second, it does nothing.

With that said, I would not recommend using this macro unless you have it in a group that's limited to a certain application. Even then, not knowing what your intended usage is, you might want to put more checks in place to make sure you really want it running.

But I don't see a way to generalize the macro for "any character," short of adding hundreds of conditions to the "until" action, which seems unrealistic.

Another approach might be to ask what you're trying to accomplish - perhaps there's a different way to get there.

-rob.

Thank you very much for your help and your macro, which of course I only understand in a rudimentary way. :rofl:

For me, “space” is also typed if it is not followed by anything. That shouldn't be the case.

Space followed by a --> space a
Space alone --> nothing

Space should only be typed if the key is pressed twice in quick succession.

And yes, letters that are pressed immediately after space should also be typed, after space of course.

I don't know in what sort of context you will want to run such a macro, but I will assume it is for when typing into a document.

The macro subspace is called by space but, despite its hastily chosen name, it isn't a subroutine. The global variable next_key is used.

For test purposes, the wait for the second key is set to 2 seconds – which of course you can change... and see what happens.

The subspace macro must be initially disabled.

One of the inspirations for this attempt was this post by Peter Lewis.

2 Likes

Ooh, that's a brilliant way to record a keystroke. Bookmarked for future use!

-rob.

1 Like

Thank you @kevinb. I'll be happy to try it out. May I ask you to post your macro? :pray:

1 Like

The upload procedure is so convoluted that I think it would be quicker for both of us if you just reconstructed the macros. :laughing: Give them a macro group that is application-specific, as @griffman also cautioned.

You might want to take the opportunity to choose better names for the global variable and the macros!

Again I'll credit Peter's post. I removed a dot to match the regex for one character, since that is what one key gives, of course.

Thinking further... yes, that does mean that some keys/characters will cause problems for the subspace macro: quotation marks, for a start.

1 Like

Okay, I'm happy to do it. My little son is ill. I have to look after him first. But I'll get back to you. :slightly_smiling_face:

1 Like

I hope he gets well soon.

I do think with short macros such as these it is often better to recreate them. It doesn't take long and it provides an opportunity to think about what the macros are doing and where they are deficient.

Speaking of which, note my comment above about quotation marks. I leave fixing that and any other peculiarities as "an exercise for the reader" too! :wink:

Thank you very much :smiley: When he's sick, he's almost the same “pain in the ass” as his father. Where does he get that from? :innocent:

Thank you also for your macros. One thing is for sure, they are doing something. But unfortunately not what I would like them to do.

The simple test is: type “space” twice in a row. Then “space” should be typed twice. But unfortunately this does not happen.

I am just testing them again... still with "0.x" seconds replaced with 2 seconds, so I don't miss what is happening...

a) I type a space and then immediately b. Result: a space, followed by the b. " b".
b) I type a space and then immediately x. Result: a space, followed by the x. " x".

I type a space and then nothing within 2 seconds. Result: nothing.

So the pair of macros satisfy the initial requirements, but now one of them has been expanded (feature creep! :stuck_out_tongue_winking_eye:) to:

If “Space” is pressed and another key or space again follows within 0.x [2] s, then type “Space”.

That's because each time the space key is pressed, it will trigger a new instance of the macro.

I tried some quick experiments with semaphore locks, and also with adding a third macro (called "hyperspace", of course) to disable the space macro and... well, I didn't solve it in the amount of further time I felt able to spend on it, but perhaps you could experiment along those or other lines.

That's if you really think that your idea is worth pursuing (I think any solution will have to be pretty rigorous to avoid bugs or side-effects) – and that KM is the way to do it. An adequate hack may be possible, but I think key input tricks can stretch KM beyond its main areas of competence and intent.

If you have a penchant for any sort of keyboard-driven tricks, I really think you should be looking at a programmable keyboard. See for instance QMK's "tap dance" feature – which I do not use, but which I suspect you might find intriguing:

You're right, I didn't explain it precisely enough. That's also because I wanted to try something out without knowing exactly where it would lead. Meanwhile, it's clear that one solved problem creates ten new ones. For reasons I don't understand, your macros work differently in different apps. To cut a long story short: I declare the experiment over. Thanks for your help though.

1 Like

It was interesting. :beers:

1 Like

I still feel we have only half the story here
Why would you want to type space and nothing happens? Is this tied to specific apps e.g. with VLC space causes the video to pause? Which apps were you thinking of?

Anyway as you can see this is helpful group and enjoy solving macro puzzles so feel free to ask again.

Actually, it's not even a quarter :wink: Since I don't want to bore anyone with nonsense, I've left out the details.

But if anyone is interested, I'm happy to elaborate.

The simplest solution to use “Space” as a “modifier” works like this:

  1. space tapped briefly --> space
  2. press and hold Space (long press) --> "modifier" down
  3. release Space --> "modifier" up

However, this setup only works if (1.) Space is typed when the key is released. And that makes everything unusable, because letters are twisted when typing quickly.

So you need a system that is a contradiction in itself. Space must be typed when the key is pressed. And Space should not be typed when the key is pressed. This can perhaps be achieved by typing Space only if it is quickly followed by another letter. If space is used as a “modifier”, you would wait briefly before pressing this letter and then space would not be typed ... Is this understandable? :rofl: And if not, it's just theoretical nonsense that can hardly work in practice.

1 Like

Sure, but giving more context in your original post might have been useful. I misunderstood where you were trying to go with all this. I should have been more explicit when I wrote "I don't know in what sort of context you will want to run such a macro, but I will assume it is for when typing into a document"! I was thinking that you would be just working with character strings when typing text, which I should have spelled out (no pun intended). In fact you wanted something with wider application than just that. And KM is definitely not a good fit.

This really is a relevant tip, then...

The sort of thing you describe is absolutely commonplace for users of programmable keyboards (keyboards that take care of all that low-level stuff when you press or hold a key and then send the resulting keycode to the computer). Here are two methods. I hope this will give you some idea. I have provided links to documentation.


1) space doubling up as a standard MacOS modifier (⇧/⌃/⌥/⌘)

If you just wanted space to double up as the (left, in this example) control key when held, you could use:

MT(MOD_LCTL, KC_SPC)

2) space doubling up as a layer switch

You can switch to different layers to get alternative keycodes from keys for as long as the switch is on. There are various ways to do this. What you describe could be handled by, for instance, the following in QMK.

From the documentation:

`LT(layer, kc)` - momentarily activates *layer* when held, and sends *kc* when tapped.

So to switch to the layer called "_LOWER" (don't worry about the name, it's just a conventional name for one of the default layers in QMK) when the key is held, but have it work as a space key when tapped, you would use:

LT(_LOWER, KC_SPC)

A graphical configurator is also available.

You can do the same thing in other systems for flashing keyboard firmware, such as ZMK and Kaleidoscope.

It's kids' stuff – if you are using the right tool for the job. Don't waste time trying to get Keyboard Maestro to do this sort of thing. It's not what it's for. Spend a little time looking into programmable keyboards. Then decide whether it's a rabbit hole that you want to investigate further. For me, it absolutely was, but it will take a significant investment of time and it won't be for everyone.

Perhaps to start with you should try the excellent Karabiner Elements, "A powerful and stable keyboard customizer for macOS". It's been a very long time since I last used it so I am not sure whether it will let you do what you need in this instance. Either way, it will widen your perspective!

1 Like