2

I'm working on an app which encoding input characters into another language (Vietnamese) and I've facing with some issue of Apple documentation.

My code to listen for keyboard events is:

func cgEventCallback (
    proxy: CGEventTapProxy,
    type: CGEventType,
    event: CGEvent,
    refcon: UnsafeMutableRawPointer?
) -> Unmanaged<CGEvent>? {
    switch type {
    case .keyDown:
        print(event.flags, CGEventFlags.maskCommand)

        if (event.flags == CGEventFlags.maskCommand) {
            print("Flag is Command")
        } else {
            print("Flag is not Command")
        }

    default:
        break
    }

    return Unmanaged.passRetained(event)
}

As they said, CGEvent's flags returns the event flags of a Quartz event. So I expect that when I type in Command + C then it should print out:

CGEventFlags(rawValue: 1048576) CGEventFlags(rawValue: 1048576)
Flag is Command

But the issue is that the printed out text is not the same as expected:

CGEventFlags(rawValue: 1048840) CGEventFlags(rawValue: 1048576)
Flag is not Command

Am I misunderstanding about something or is there any way which could help me know what is the current flag of the event?

1 Answer 1

1

event.flags is an OptionSet representing all flags for the current event. There can be more than one flag set, and that is what is happening in your case:

event.flags  = 1048840 (decimal) = 100108 (hex)
.maskCommand = 1048576 (decimal) = 100000 (hex)

So the correct test is

if event.flags.contains(.maskCommand) { ... }

instead of testing for equality.

9
  • Thanks for your answer @Martin R, I just have a small concern about this is that if I want to know the flags is currently in another OptionSet, e.g: [.maskCommand, .maskControl], to know that user is entering Control key or Command key? Jan 24, 2021 at 13:40
  • @ToanQuocHo: You can do event.flags.isSuperset(of: [.maskCommand, .maskControl]) to test if the event flags contain any of the given flags. Is that what you are looking for?
    – Martin R
    Jan 24, 2021 at 13:49
  • Yeah, I think something like that @Martin R, but something included inside flags which I don't know what it is. When I enter Command + C I hope that event.flags.isSuperset(of: [.maskCommand, .maskControl]) to be true, but it's not. Jan 24, 2021 at 13:54
  • 1
    @ToanQuocHo I never said to use it as it is. Just a reference on how to switch an OptionSet.
    – Leo Dabus
    Jan 24, 2021 at 15:06
  • 1
    Got that @LeoDabus, I've tried with isDisjoint and it works in my case. It was inspired by your comment though, thank you so much :D Jan 24, 2021 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.