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

时间:2019-10-21 14:56:31

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

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

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

谢谢。

1 个答案:

答案 0 :(得分: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)