Show keyboard viewer

Hi, I was looking here in the forum for a way to open the keyboard viewer
And I found the following macro:
https://forum.keyboardmaestro.com/t/how-to-bring-up-the-keyboard-viewer/21509

Unfortunately it doesn't work, and when I looked for the specific file it wasn't in this path.

I guess because this post is old and I'm in Ventura osx, maybe Apple changed the path for keyboard viewer.app
any suggestions how to open this keyboard from macro?

1 Like

Hello, @keyboard

I wrote a small AppleScript script as a playground exercise several years ago. It toggles the viewer, although it may appear superfluous to make it hide programmatically rather than clicking on x.
Since it employs GUI scripting, expect it to break in the event that the more current macOS has changed the GUI structure.

ignoring case
tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			tell (the first menu bar item whose description contains "text input" or description contains "text")
				perform action "AXPress"
				repeat until menu 1 exists
					delay 0.1
				end repeat
				tell menu 1
					if exists menu item "show keyboard viewer" then
						set TargetMenuItem to "show keyboard viewer"
					else
						set TargetMenuItem to "hide keyboard viewer"
					end if
					tell menu item TargetMenuItem
						perform action "AXPress"
						if TargetMenuItem is "hide keyboard viewer" then
							do shell script "killall ABRT KeyboardViewer" -- the command is necessary because after closing the keyboard's window its process is still running. The loop below checks for its status.
							set ProcessExists to true
							repeat until ProcessExists is false
								set ProcessExists to exists application process "Keyboard Viewer" of application "System Events"
								delay 0.1
							end repeat
						end if
					end tell
				end tell
			end tell
		end tell
	end tell
end tell
end ignoring
1 Like

Keyboard Viewer the app disappeared a while ago. I had a look but couldn't find a command line method for opening its replacement -- but I'd love to be proved wrong!

Yep, while everything used to be a child of SystemUIServer they're now split across multiple processes -- SystemUIServer, ControlCenter... And, in this case, TextInputMenuAgent.

I was thrown for a while, not realising that TextInputMenuAgent has two menu bars -- the first seems to be another route to the Apple Menu and the application menus, the second holds the icons etc at the other end of the menu bar.

Because the menu item you want becomes "Hide Keyboard Viewer" after you've activated the viewer, this will act as a toggle.

tell application "System Events"
	tell process "TextInputMenuAgent"
		tell menu bar 2
			tell item 1 of (every menu bar item whose accessibility description is "Show Emoji & Symbols")
				click
				tell menu 1
					if exists menu item "Show Keyboard Viewer" then
						click menu item "Show Keyboard Viewer"
					else
						click menu item "Hide Keyboard Viewer"
					end if
				end tell
			end tell
		end tell
	end tell
end tell

As a macro:
Toggle Keyboard Viewer.kmmacros (2.2 KB)

Image

Unfortunately, GUI-scripting via System Events is never fast...

1 Like

Fast enough for me.

thanks guys for replay..
I played a bit with the keyboard viewer settings and found an option for a hot corner, the truth is it works amazingly. I changed the default dwell time to 0.25ms and it gives a great solution.

take a look:
https://streamable.com/yidbhh

Yes -- I used too broad a brush there.

It does depend on what you are doing under what OS, and particularly on whether you're able to use the ignoring application responses trick.

The above macro takes 1.8 seconds to run for me on Ventura, then there's a short delay for Keyboard Viewer to appear after the menu click. ignoring breaks the macro for me. Yours looks like an earlier OS, which could explain why it's much faster for you.

That's neat. I hadn't realised "Home Panel" was a synonym for "Keyboard Viewer".

I've a couple of people at work that this could help so, on their behalf -- thanks!

No problems, have fun!
You have currently improved the run time from 1.8 sec to 0.25 :wink: