Shortcut to toggle KeyboardViewer with AppleScript on Big Sur
This works for me on macOS Big Sur.
if application &quot;System Preferences&quot; is running then do shell script &quot;killall 'System Preferences'&quot;
repeat until application &quot;System Preferences&quot; is not running
    delay 0.1
end repeat

tell application &quot;System Preferences&quot;
    reveal anchor &quot;Virtual_Keyboard&quot; of pane &quot;com.apple.preference.universalaccess&quot;
end tell

tell application &quot;System Events&quot; to tell application process &quot;System Preferences&quot;
    repeat until checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 ¬
        of group 1 of window &quot;Accessibility&quot; exists
        delay 0.1
    end repeat
    set theValue to value of checkbox &quot;Enable Accessibility Keyboard&quot; of tab group ¬
        1 of group 1 of window &quot;Accessibility&quot;
    delay 0.1
    if theValue = 1 then
        set frontmost to true
        repeat until frontmost
            delay 0.1
        end repeat
        click checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 of ¬
            group 1 of window &quot;Accessibility&quot;
        delay 0.1
        if UI element &quot;OK&quot; of sheet 1 of window &quot;Accessibility&quot; exists then
            click UI element &quot;OK&quot; of sheet 1 of window &quot;Accessibility&quot;
        end if
    else
        click checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 of ¬
            group 1 of window &quot;Accessibility&quot;
    end if
end tell

do shell script &quot;killall 'System Preferences'&quot;

SIDENOTE: My reason for using the the do shell script &quot;killall 'System Preferences'&quot; command rather than tell application &quot;System Preferences&quot; to quit is... Let's say, for what ever reason, System Preferences.app is already activated (with, for example, window Sharing/ Remote Management/ Computer Settings open) but not visible or not front most or whatever. In Script Editor, you try to run tell application &quot;System Preferences&quot; to reveal anchor &quot;SpeakableItems&quot; of pane id &quot;com.apple.preference.universalaccess&quot; That command completes with no obvious errors but when you switch over to System Preferences, your &quot;revealed anchor&quot; is not revealed. Ok, no big deal, easy fix. I'll just insert a tell application &quot;System Preferences&quot; to quit command prior to the reveal anchorcommand. Running the updated code this time, you get a (userCanceledErr:-128) error. All of this hassle is because that secondary drop down window in System Preferences is open. In my opinion, the do shell script &quot;killall 'System Preferences'&quot; command seems to be the best solution.

Here is another option if you have the “Show Accessibility status in menu bar” checked in System Preferences/Accessibility. It's short, it's sweet, but it does use UI scripting.
tell application &quot;System Events&quot;
    click menu bar item &quot;Accessibility Shortcuts&quot; of menu bar 1 ¬
        of application process &quot;ControlCenter&quot;
    repeat until checkbox &quot;Accessibility Keyboard&quot; of group 1 of ¬
        window &quot;Control Center&quot; of application process &quot;ControlCenter&quot; exists
        delay 0.1
    end repeat
    click checkbox &quot;Accessibility Keyboard&quot; of group 1 of ¬
        window &quot;Control Center&quot; of application process &quot;ControlCenter&quot;
    delay 0.1
    key code 53 -- escape key
end tell