swift - 从 NSButton 操作调用表情符号和符号选择器

标签 swift xcode macos emoji

我是 Swift 的新手,目前正在为 macOS 开发一个小型消息传递应用程序。基本应用程序已完成,但我正在尝试添加表情符号选择器。

我想添加一个按钮来打开“表情符号和符号”窗口。这会在启动应用程序时自动添加到“编辑”菜单,但我希望通过按下 NSButton(就像在 native Mac Messages 应用程序中一样)让它弹出。

有没有办法调用系统函数来显示表情符号选择器,或者有办法模拟键盘快捷键(ctrl+cmd+space)?如果是这样,我需要采取哪些步骤来实现它?

最佳答案

多亏了使用键盘快捷键的建议。

下面是我使用的代码。我在模拟按下两个修饰键时遇到了问题,因此解决方案是创建自定义 CGEventFlags 项。

// Setup a custom CGEventFlags Item with value of .MaskControl and .MaskCommand
    let commandControlMask = (CGEventFlags.MaskCommand.rawValue | CGEventFlags.MaskControl.rawValue)
    let commandControlMaskFlags = CGEventFlags(rawValue: commandControlMask)!

    // Press Space key once
    let space = CGEventSourceCreate(.HIDSystemState)
    let keyDown = CGEventCreateKeyboardEvent(space, 49 as CGKeyCode, true)
    CGEventSetFlags(keyDown, commandControlMaskFlags)
    CGEventPost(.CGHIDEventTap, keyDown)
    let keyUp = CGEventCreateKeyboardEvent(space, 49 as CGKeyCode, false)
    CGEventSetFlags(keyUp, commandControlMaskFlags)
    CGEventPost(.CGHIDEventTap, keyUp)

关于swift - 从 NSButton 操作调用表情符号和符号选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611925/

相关文章:

swift - 使用Any类型是一个好的解决方案时,有没有具体的例子?

cocoa - NSTableView - NSButtonCell 数据源类型?

ios - 快速关闭不能覆盖任何

xcode - 由 Xcode 版本 13 构建的应用程序在 ios 12 上崩溃

iphone - XCode/iOS : Storyboard Segue event does not work after adding UIGestureRecognizerDelegate to UIViewController

ios - Xcode 5 崩溃——Xcode 意外退出

macos - 致命的 : Failed to start gitlab-runner: "launchctl" failed with stderr: Load failed: 5: Input/output error

android - 如何让 Android 命令行工具工作?

ios - 将 subview 缩放到父级大小

ios - 解析.com includeKey : not working