我创建了一个可可应用程序,它有一个带有文本字段的窗口来获取用户输入,一个小的键盘图标按钮可以调出键盘查看器。当用户单击确定或取消按钮完成时,我想隐藏键盘查看器。我所做的如下:
//action for keyboard-icon button
-(IBAction)input:(id)sender
{
[self toggleKeyboard:YES];
}
//action for Cancel button
-(IBAction)cancel:(id)sender
{
[self toggleKeyboard:NO];
[NSApp abortModal];
[[self window] orderOut: self];
}
//action for OK button
-(IBAction)ok:(id)sender
{
[self toggleKeyboard:NO];
[NSApp stopModal];
[[self window] orderOut: self];
}
-(void)toggleKeyboard:(BOOL)show
{
NSDictionary *property = [NSDictionary dictionaryWithObject:(NSString*)kTISTypeKeyboardViewer
forKey:(NSString*)kTISPropertyInputSourceType];
NSArray *sources = (NSArray*)TISCreateInputSourceList((CFDictionaryRef)property, false);
TISInputSourceRef keyboardViewer = (TISInputSourceRef)[sources objectAtIndex:0];
if (show == YES)
{
TISSelectInputSource(keyboardViewer);
}
else
{
TISDeselectInputSource(keyboardViewer);
}
CFRelease((CFTypeRef)sources);
}
我可以成功启动键盘查看器,但 TISDeselectInputSource 无法始终隐藏它。