【问题标题】:macOS Catalina - Onscreen (virtual) Keyboard not workingmacOS Catalina - 屏幕(虚拟)键盘不工作
【发布时间】:2020-02-17 16:48:49
【问题描述】:

我一直在使用下面的代码来显示虚拟屏幕键盘,到目前为止工作良好,直到 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);
}

请告知是否有人遇到过类似问题并且有任何解决方案/解决方法可用。

谢谢。

【问题讨论】:

  • 似乎 Apple 完全放弃了虚拟键盘,而现在他们只有辅助功能键盘。不确定如何以编程方式提出这一点。

标签: objective-c macos macos-carbon macos-catalina


【解决方案1】:

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

这样的东西应该可以工作(可以使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多