macOS Catalina - 屏幕(虚拟)键盘不工作

问题描述 投票:0回答:2

我一直在使用下面的代码来显示虚拟屏幕键盘,到目前为止在 10.14 之前运行良好,但它不再适用于 Catalina (10.15)。在 Catalina 上,它不再能够创建输入源并且它始终为空 -

+ (void) showHideVirtualKeyboard:(BOOL) shouldShow {


    NSDictionary *property = [NSDictionary dictionaryWithObject:(NSString*)kTISTypeKeyboardViewer
                                                         forKey:(NSString*)kTISPropertyInputSourceType];
    NSLog(@"showHideVirtualKeyboard my dictionary is - %@",property);
    NSArray *sources = (__bridge NSArray*)TISCreateInputSourceList((__bridge CFDictionaryRef)property, false);
    if([sources count] == 0) {
        DBLogError(@"ToggleKeyboard: no input keyboard source type found...");
        DBLogError(@"ToggleKeyboard: Trying to create keyboard soruce type");
        sources = (__bridge NSArray*)TISCreateInputSourceList((__bridge CFDictionaryRef)property, true);
        if ([sources count]==0) {
            DBLogError(@"ToggleKeyboard: Still can't find any input sources so nothing to...");
            if(sources)
                CFRelease((CFTypeRef)sources);
            return;
        }
    }

    TISInputSourceRef keyboardViewer = (__bridge TISInputSourceRef)[sources objectAtIndex:0];
    //DBLogInfo(@"********** received sources are %@",keyboardViewer);
    int osStatus;
    //let's show hide keyboard
    if (shouldShow == YES){
        CFBooleanRef enabled = TISGetInputSourceProperty(keyboardViewer, kTISPropertyInputSourceIsEnabled);
        if (enabled == kCFBooleanFalse)
            TISEnableInputSource(keyboardViewer);
        // DBLogInfo(@"kTISPropertyInputSourceIsEnabled = %@",(enabled == kCFBooleanFalse)?@"false":@"true");
        osStatus = TISSelectInputSource(keyboardViewer);
    }
    else
        osStatus = TISDeselectInputSource(keyboardViewer);

    if(osStatus !=noErr)
        DBLogInfo(@"ToggleKeyboard: Received errored OSStatus and it is (%d) ",osStatus);
    if(sources)
        CFRelease((CFTypeRef)sources);
}

如果有人遇到过类似的问题并且有任何解决方案/解决方法,请告知。

谢谢。

objective-c macos macos-carbon macos-catalina
2个回答
0
投票

对于 Catalina,Apple 现在使用辅助功能键盘作为虚拟键盘(键盘查看器)。而我能想出以编程方式显示这一点的唯一方法似乎是使用 Apple Script。

像这样的东西应该可以工作(可以用 NSAppleScript 调用或通过从 C/C++ 代码执行 osascript 调用):

activate application "System Preferences"
tell application "System Preferences"
    reveal anchor "Virtual_Keyboard" in pane id "com.apple.preference.universalaccess"
end tell

tell application "System Events"
    tell process "System Preferences"
        repeat 20 times
            if (exists checkbox "Enable Accessibility Keyboard" of tab group 1 of group 1 of window 1) then
                click checkbox "Enable Accessibility Keyboard" of tab group 1 of group 1 of window 1
                exit repeat
            end if
            delay 1
        end repeat
    end tell
end tell
tell application "System Preferences" to quit

请注意,要使用此功能,用户需要授权您的应用使用自动化和辅助功能。此外,您的应用程序还应该在 Info.plist

中声明一个目的字符串 (NSAppleEventsUsageDescription)

0
投票

我让这个版本在 Ventura 13.2.1 中工作:

tell application "System Settings"
    activate
    delay 0.2
end tell
tell application "System Events"
    tell application process "System Settings"
        tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1
            select row 16
            delay 0.2
        end tell
        tell group 3 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
            click button 2
            delay 0.2
            click checkbox "Accessibility Keyboard"
        end tell
    end tell
end tell
tell application "System Settings" to quit

这很脆弱,我不希望它能在另一次操作系统升级后存活下来。您可能还需要更长的延迟时间。 dparakh 采取的方法,使用重复而不是延迟可能更好。

© www.soinside.com 2019 - 2024. All rights reserved.