Is there a way to count how many times a key was pressed since boot?

Alright. I'm asking this to solely solve this one problem. I've got a shortcut assigned to change "Full Keyboard Access" setting inside "Keyboard" menu.

I need some way to track is it on or off. The only way I can think of is to count odd and even presses of hotkey issuing appropriate messages. I got a BitBar but don't how to utilise it.

Any thoughts on this will be appreciated. Thanks!

For a raw count of the keypresses, you could do something like this:

If you want to track the change in the Tab key's behavior (though I'd argue it'll be evident the next time you press the button ;)) You'll probably need to count odds and evens, like you said. There's probably a dead simple way to do that in AppleScript etc.

I'm pretty sure that you can get the status (value) of the various System Prefs using a script. I think this would work much better than counting keystrokes.

Sorry, but I don't have the script at my fingertips. If @ccstone or someone doesn't come along soon to help, I'll try to find the script.

Meanwhile, you might try a google of "AppleScript system preferences"

Thanks, guys! @cfriend thanks for Macro. @JMichaelTX I’ll be much appreciated for a script!

I will investigate into Applescript more. One more question - how should I guess an option name in terms of OS? I saw some guides for changing options and they includes terminal commands with How did they get them?

Thanks again!

Found somewhat “almost-there” solution. I can check current status of this option by typing in terminal:

defaults read -g AppleKeyboardUIMode

It will return 0 (off) or 2 (on) dependent of current setting.

Question is:

How can I track its status if KM reassign my defined shortcut (Hyperkey + F) to itself not allowing OS X shortcut (which is currently switching that thing happily)?

Is there a way to allow other hotkey to work and let KM just pass-by holding hands?

This macro should do the job.
It returns a "1" if "All Controls" is ON (selected); "0" if OFF.

Please give it a try and let us know if it works for you.


##example Output

##Macro Library   Get Status/Value of Full Access KB Preferences @Example

<a class="attachment" href="/uploads/default/original/2X/b/b0f6618358b525b9403af51d4886ffcba1a8a93a.kmmacros">Get Status-Value of Full Access KB Preferences @Example.kmmacros</a> (12 KB)





* **Get the Status of the "All Controls" Radio Button on 
System Preferences > Keyboard > Shortcuts > Full Keyboard Access**
* RETURNS "1" if the Button is ON;  "0" if it is OFF.


1. Trigger this macro from anywhere
(be sure to do "Macro Setup" first)


* Assign a trigger


* Uses the macOS System Events to perform UI scripting, and get value of the selected button.

  * Any Action in magenta color is designed to be changed by end-user
  * To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
  * GREEN      -- Key Comments designed to highlight main sections of macro
  * MAGENTA -- Actions designed to be customized by user
  * YELLOW   -- Primary Actions (usually the main purpose of the macro)
  * ORANGE  -- Actions that permanently destroy Varibles or Clipboards

(1)  Keyboard Maestro Ver 7.2.1+
(2)  Yosemite (10.10.5)+


<img src="/uploads/default/original/2X/4/4344c921caf110803dc2cd69b91b21040fe3f729.png" width="589" height="1001">


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property ptyScriptName : "Determine if 'All Controls' is set for Full Access TAB Behavior"
property ptyScriptVer : "2.0"
property ptyScriptDate : "2017-03-24"
property ptyScriptAuthor : "JMichaelTX"

PURPOSE: Get & Return Value of "Full Access" Radio button on
System Preferences > Keyboard > Shortcuts > Full Keyboard Access

RETURNS: "0" if OFF; "1" if ON.

KM VARIABLES REQUIRED: (must be set before calling this script)

KM VARIABLES SET: (by this script)

property _CR : return
property _TAB : tab

--  Will be either actual results, or Error message
set scriptResults to "TBD"

  set frontApp to path to frontmost application as text -- use for dialogs
  --    MUST be "Text boxes and lists only" OR  "All controls"
  set buttonToCheck to "All controls"
  with timeout of 10 seconds
    tell application "System Preferences"
      if not running then run # Works around 'activate' bug.
      reveal anchor "shortcutsTab" of pane id ""
    end tell
    tell application "System Events"
      tell process "System Preferences"
        set frontmost to true
        tell (first window whose subrole is "AXStandardWindow")
          tell tab group 1
            tell radio group 1
              set rg1Elem to UI elements
              tell radio button buttonToCheck
                set buttonValue to value of attribute "AXValue"
              end tell
            end tell -- radio group 1
          end tell -- tab group 1
        end tell --  window 1
      end tell --  "System Preferences"
    end tell --  "System Events"
    --- Comment-out Next Statement if you want to see results ---
    tell application "System Preferences" to close window 1
  end timeout
  set scriptResults to buttonValue
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
    -- Some other error, just use system errMsg
  end if
  set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
    & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
    & "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return scriptResults


<AXApplication: “System Preferences”>
 <AXWindow: “Keyboard”>
    <AXRadioButton: “Text boxes and lists only”>

   AXEnabled:  “1”
   AXParent:  “<AXRadioGroup>”
   AXSize:  “w=193 h=18”
   AXFocused (W):  “0”
   AXRole:  “AXRadioButton”
   AXTopLevelUIElement:  “<AXWindow: “Keyboard”>”
   AXHelp:  “(null)”
   AXValue:  “1”
   AXPosition:  “x=1394 y=530”
   AXWindow:  “<AXWindow: “Keyboard”>”
   AXRoleDescription:  “radio button”
   AXTitle:  “Text boxes and lists only”
   AXFrame:  “x=1394 y=530 w=193 h=18”

   AXPress - press

1 Like

Wow @JMichaelTX

Much thanks for putting your effort in! It works perfectly!

Just to note - should provide a “Type Keystroke” action before running your script.

Thanks again!

I'm glad it solves your problem.

I'm not sure what you mean by this. The macro runs fine for me with Keyboard Maestro 7.3.1 (7.3.1) on macOS 10.11.6.

I’m using Yosemite 10.10.5. If I am leaving script alone as it is, it doesn’t switch state properly. I mean, it doesn’t switch at all. I have inserted “Type A Keystroke” action to solve this.

The purpose of the script is NOT to change/switch the Full Access setting.
The purpose is just to report the current status of the "All Controls" choice.

Isn't this what you asked for:

If what you want is to set "All Controls" regardless of the current setting, I actually have another script that will do that.

Yes, I totally understand this. This is what I was asking for, thats right. Thanks for your job! It works as it should!

I've described steps taken to make a full working macro with your solution to keep track of setting.

If what you want is to set "All Controls" regardless of the current setting, I actually have another script that will do that.

Thanks, I'm totally fine with a current setup now.

1 Like

Probably I’m missing something, but you have already provided the solution yourself with

defaults read -g AppleKeyboardUIMode

To further use the output of that shell script in KM just save the results to a variable:

Here is an example of a complete macro:

Status of Full Keyboard Access.kmmacros (3.5 KB)

  1. The macro takes the result of the shell script

  2. For better readability it converts the result to text:

  • if it’s 0 it converts the variable to the text “Text boxes and lists only”
  • if it’s 2 it converts the variable to the text “All controls”
  1. It displays the result text in a notification:

Of course, the actions in the macro are just examples of what you can do with the result of the shell script, and you can replace them with actions of your choice.

1 Like

I like @Tom's method/macro better than the one I posted.

Although I generally stay away from shell scripts, in this case it is only reading data, AND it avoids use of UI scripting.

So, in this case, shell scripting trumps UI scripting.

Actually, made Tom's conclusions myself right after download of your script, @JMichaelTX (Thanks @Tom btw ) :slight_smile: Just wanted to appreciate your work, @JMichaelTX.

I'm start doing it for my TextExpander popup window (so I can use keyboard to select things I need. Which is TextExpander somehow misses)

Yes, I posted the macro because I had the impression that your main problem was that you didn’t know how to use the output of the shell script in KM. (Since you had already found the correct shell script.)

FYI, I did not see this post when I posted/uploaded my macro. :wink: