IdleKeyCounter Macro (v9.0.2)

IdleKeyCounter Macro (v9.0.2)

I'll bet this will be the shortest useful macro in the history of KM. I wrote it because I wanted an equivalent to the IDLE() function that wasn't triggered by mouse movement, because mouse movements can be accidentally triggered by bumping the table or by a large truck driving past my house. This macro solves that by resetting a variable called IdleKey only when a character key (not by any mouse motion) is typed by the user. In my opinion this is more robust than the IDLE() function.

Since there is no ability to create a function in KM, if you have this macro active then the equivalent to "IDLE()" would be "MS-IdleKey". That's four extra characters, so it's not too bad. I suppose if you wanted to rename the variable IdleKey to T then you could write "MS-T" and it would be really short.

It might be nice to detect not only character keys, but also modifier keys and function keys. I don't think it's possible to trigger on a modifier key with KM. It is possible to trigger on a function key, however there's a small (not insurmountable) problem with that. The hotkey detection trigger eats the key it triggers on. I think KM should be modified with a flag to the HotKey detection trigger to let us "not eat" the key, just as there is with the string trigger used by this macro. If anyone asks I can enhance this macro to support function keys, but it would detract from the sheer beauty of this short macro.

I place this code into the public domain. It's free to use. No attribution required.

IdleKeyCounter.kmmacros (1.5 KB)

What is the value "MS" in this macro?

Time in milliseconds since system startup.

See the following pages:
function:MILLISECONDS [Keyboard Maestro Wiki]
Keyboard Maestro 6 Documentation: Calculations

So, MS is some magic abbreviation for MILLISECONDS()?

I think it's an antiquated way of doing things, kind of like how technically you can reference variables without the word variable, I.e. %myVar% instead of %Variable%myVar%.

I say that because I could only find it in KM6 documentation. In KM10 wiki pages it's referred to as a function listed as MILLISECONDS().

But I could be totally off-base here. :sweat_smile:

Thanks. MS is not in the list of Variables in KM Preferences.

Well it’s definitely not a variable so it wouldn’t be listed there. I imagine you can find it in either Functions or Calculations but it might be listed as MILLISECONDS() if the MS version is deprecated.

Yes, once I knew that MS meant MILLISECONDS() it was easy.

1 Like

This is very cool, indeed. And I'm using it. However, I also want to track mouse clicks. Any ideas on how to do that?

[Why? IDLE() is frequently resetting its timer when other KM's are running in the background. I don't know what is causing this, perhaps it is one of my macros using the "Type a Keystroke" action, but it could be something else. If I could list the specific keys that I'm using in a hot key trigger (like ^0 and ^1) I could exclude those from your IdleKeyCounter macro, but I haven't found a way to do that.]