How to Create a Duplicate or a Prior Key?

Hello Keyboard Maestro Community,

I like to type in chords and designed a custom keyboard layout. To type chords more consistently, I created a duplicate key thats value is the last key pressed. This lets me type words like 'good' or 'pressed' without double tapping(which i reserve for capitalization). I did this on Windows using Auto Hot Keys and they had a variable called A_PriorKey, which is 'The name of the last key which was pressed prior to the most recent key-press or key-release'. So what I did was use a '&' as my duplicate key, so whenever I typed a word containing 's&' it would produce 'ss' like in 'pressed'. This is what the code looks like.

  ; preceding key
  key := A_PriorKey
  Send {%key%}

I searched for a prior key, duplicate and repeat key but found nothing useful. I made a quick hack with the following script


It does not work every time though which is frustrating. 20 times: pres& pres& press press pres& press press press press pres& press pres& press press press pres& pres& press press press
7/20 times resulted in the wrong text. I think it used to be better... maybe an update happened or my mac is getting worse idk.

If there is a better way to get this accomplished, please let me know. Thank you! :grinning:

Oh, and I forgot to mention, I would have to create a bunch of those duplicate key scripts for each letter which may impact performance?
Screen Shot 2021-10-11 at 6.51.12 PM

I found a solution. For each letter key, I have it type the same letter press and store a variable in an instance variable. I then have the instance variable printed when I hit a set key. This approach has been very reliable

Screen Shot 2021-10-12 at 8.24.53 PM

Screen Shot 2021-10-12 at 8.24.40 PM

When I read your solution I thought “that wouldn’t work with an instance variable as they do not persist between macro calls”. Looking at your macro I see your variable is called instancedup which is actually a global variable and that’s why it works for you.

In the KM wiki, you’ll see that the name of an instance variable has to start with Instance which is case sensitive.

It is always good practice as well to refer to variable tokens as
%Variable%name of KM variable%
as then you have less chance of confusing them with KM tokens.

Just saying this so you’ll have a more enjoyable experience with KM in the future!


Thanks, I updated %Variable%dup% to be more idiomatic