LaunchMenu / node-global-key-listener

A package to listen to global key events
MIT License
89 stars 11 forks source link

Key Server crashed/exited after receive one keyboard/mouse event. #41

Open aztack opened 4 months ago

aztack commented 4 months ago

MacOS 14.3 (23D56), M3 Chip. Node 20.2.0

Just install node-global-key-listener and touch index.js, paste demo code. The node process exited after console.log out a keyboard/mouse event.

In the Activity Monitor, the Mac Key Server is disappeared after click mouse or tap any key.

But there no problem or crash when running the key server from command line.

sancarn commented 4 months ago

Hi aztack, can you try the latest version and see if you still have issues?


If you still have issues, can you try the following:

can you try pulling this repo and running the following commands:

npm install
npm run build
npm compile-mac
npm run test

and see if the tests run? I imagine the package just hasn't been built for mac in the published npm version.

aztack commented 2 months ago

Sorry for replying to your post so late. I tried above commands but got errors on the compile-mac step:

➜  node-global-key-listener git:(main) npm run compile-mac

> node-global-key-listener@0.3.0 compile-mac
> swiftc src/bin/MacKeyServer/main.swift -o bin/MacKeyServer

src/bin/MacKeyServer/main.swift:197:34: error: cannot find type 'CGEvent' in scope
func getModifierDownState(event: CGEvent, keyCode: Int64) -> Bool {
                                 ^~~~~~~
src/bin/MacKeyServer/main.swift:225:133: error: cannot find type 'CGEvent' in scope
func myCGEventTapCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
                                                                                                                                    ^~~~~~~
src/bin/MacKeyServer/main.swift:225:34: error: cannot find type 'CGEventTapProxy' in scope
func myCGEventTapCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
                                 ^~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:225:57: error: cannot find type 'CGEventType' in scope
func myCGEventTapCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
                                                        ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:225:77: error: cannot find type 'CGEvent' in scope
func myCGEventTapCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
                                                                            ^~~~~~~
src/bin/MacKeyServer/main.swift:282:9: error: cannot find 'CGEventType' in scope
  (1 << CGEventType.flagsChanged.rawValue)
        ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:283:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.keyDown.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:284:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.keyUp.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:287:9: error: cannot find 'CGEventType' in scope
  (1 << CGEventType.leftMouseDown.rawValue)
        ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:288:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.leftMouseUp.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:289:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.rightMouseDown.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:290:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.rightMouseUp.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:291:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.otherMouseDown.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:292:11: error: cannot find 'CGEventType' in scope
  | (1 << CGEventType.otherMouseUp.rawValue)
          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:286:5: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
let mouseEventMask =
    ^~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:300:22: error: cannot find 'CGEvent' in scope
guard let eventTap = CGEvent.tapCreate(
                     ^~~~~~~
src/bin/MacKeyServer/main.swift:304:27: error: cannot find 'CGEventMask' in scope
        eventsOfInterest: CGEventMask(eventMask),
                          ^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:301:15: error: cannot infer contextual base in reference to member 'cgSessionEventTap'
        tap: .cgSessionEventTap,
             ~^~~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:302:17: error: cannot infer contextual base in reference to member 'headInsertEventTap'
        place: .headInsertEventTap,
               ~^~~~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:303:19: error: cannot infer contextual base in reference to member 'defaultTap'
        options: .defaultTap,
                 ~^~~~~~~~~~
src/bin/MacKeyServer/main.swift:306:19: error: 'nil' requires a contextual type
        userInfo: nil
                  ^
src/bin/MacKeyServer/main.swift:326:1: error: cannot find 'CGEvent' in scope
CGEvent.tapEnable(tap: eventTap, enable: true)
^~~~~~~
src/bin/MacKeyServer/main.swift:200:42: error: cannot infer contextual base in reference to member 'maskCommand'
            return event.flags.contains(.maskCommand);
                                        ~^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:202:42: error: cannot infer contextual base in reference to member 'maskShift'
            return event.flags.contains(.maskShift);
                                        ~^~~~~~~~~
src/bin/MacKeyServer/main.swift:204:42: error: cannot infer contextual base in reference to member 'maskControl'
            return event.flags.contains(.maskControl);
                                        ~^~~~~~~~~~~
src/bin/MacKeyServer/main.swift:206:42: error: cannot infer contextual base in reference to member 'maskAlternate'
            return event.flags.contains(.maskAlternate);
                                        ~^~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:208:42: error: cannot infer contextual base in reference to member 'maskAlphaShift'
            return event.flags.contains(.maskAlphaShift);
                                        ~^~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:210:42: error: cannot infer contextual base in reference to member 'maskSecondaryFn'
            return event.flags.contains(.maskSecondaryFn);
                                        ~^~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:212:42: error: cannot infer contextual base in reference to member 'maskHelp'
            return event.flags.contains(.maskHelp);
                                        ~^~~~~~~~
src/bin/MacKeyServer/main.swift:226:10: error: reference to member 'keyDown' cannot be resolved without a contextual type
    if [.keyDown , .keyUp].contains(type) {
         ^
src/bin/MacKeyServer/main.swift:226:21: error: reference to member 'keyUp' cannot be resolved without a contextual type
    if [.keyDown , .keyUp].contains(type) {
                    ^
src/bin/MacKeyServer/main.swift:226:28: error: type 'Any' cannot conform to 'Equatable'
    if [.keyDown , .keyUp].contains(type) {
                           ^
src/bin/MacKeyServer/main.swift:226:28: note: only concrete types such as structs, enums and classes can conform to protocols
    if [.keyDown , .keyUp].contains(type) {
                           ^
src/bin/MacKeyServer/main.swift:226:28: note: required by referencing instance method 'contains' on 'Sequence' where 'Self.Element' = 'Any'
    if [.keyDown , .keyUp].contains(type) {
                           ^
src/bin/MacKeyServer/main.swift:227:51: error: cannot infer contextual base in reference to member 'keyboardEventKeycode'
        let keyCode = event.getIntegerValueField(.keyboardEventKeycode) //CGKeyCode
                                                 ~^~~~~~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:237:17: error: reference to member 'flagsChanged' cannot be resolved without a contextual type
    } else if [.flagsChanged].contains(type) {
                ^
src/bin/MacKeyServer/main.swift:237:31: error: type 'Any' cannot conform to 'Equatable'
    } else if [.flagsChanged].contains(type) {
                              ^
src/bin/MacKeyServer/main.swift:237:31: note: only concrete types such as structs, enums and classes can conform to protocols
    } else if [.flagsChanged].contains(type) {
                              ^
src/bin/MacKeyServer/main.swift:237:31: note: required by referencing instance method 'contains' on 'Sequence' where 'Self.Element' = 'Any'
    } else if [.flagsChanged].contains(type) {
                              ^
src/bin/MacKeyServer/main.swift:239:51: error: cannot infer contextual base in reference to member 'keyboardEventKeycode'
        let keyCode = event.getIntegerValueField(.keyboardEventKeycode)
                                                 ~^~~~~~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:251:18: error: reference to member 'leftMouseDown' cannot be resolved without a contextual type
                .leftMouseDown,
                 ^
src/bin/MacKeyServer/main.swift:252:18: error: reference to member 'leftMouseUp' cannot be resolved without a contextual type
                .leftMouseUp,
                 ^
src/bin/MacKeyServer/main.swift:253:18: error: reference to member 'rightMouseDown' cannot be resolved without a contextual type
                .rightMouseDown,
                 ^
src/bin/MacKeyServer/main.swift:254:18: error: reference to member 'rightMouseUp' cannot be resolved without a contextual type
                .rightMouseUp,
                 ^
src/bin/MacKeyServer/main.swift:255:18: error: reference to member 'otherMouseDown' cannot be resolved without a contextual type
                .otherMouseDown,
                 ^
src/bin/MacKeyServer/main.swift:256:18: error: reference to member 'otherMouseUp' cannot be resolved without a contextual type
                .otherMouseUp,
                 ^
src/bin/MacKeyServer/main.swift:257:17: error: type 'Any' cannot conform to 'Equatable'
              ].contains(type) {
                ^
src/bin/MacKeyServer/main.swift:257:17: note: only concrete types such as structs, enums and classes can conform to protocols
              ].contains(type) {
                ^
src/bin/MacKeyServer/main.swift:257:17: note: required by referencing instance method 'contains' on 'Sequence' where 'Self.Element' = 'Any'
              ].contains(type) {
                ^
src/bin/MacKeyServer/main.swift:259:12: error: reference to member 'leftMouseDown' cannot be resolved without a contextual type
          .leftMouseDown,
           ^
src/bin/MacKeyServer/main.swift:260:12: error: reference to member 'rightMouseDown' cannot be resolved without a contextual type
          .rightMouseDown,
           ^
src/bin/MacKeyServer/main.swift:261:12: error: reference to member 'otherMouseDown' cannot be resolved without a contextual type
          .otherMouseDown,
           ^
src/bin/MacKeyServer/main.swift:262:11: error: type 'Any' cannot conform to 'Equatable'
        ].contains(type)
          ^
src/bin/MacKeyServer/main.swift:262:11: note: only concrete types such as structs, enums and classes can conform to protocols
        ].contains(type)
          ^
src/bin/MacKeyServer/main.swift:262:11: note: required by referencing instance method 'contains' on 'Sequence' where 'Self.Element' = 'Any'
        ].contains(type)
          ^
src/bin/MacKeyServer/main.swift:263:51: error: cannot infer contextual base in reference to member 'mouseEventButtonNumber'
        let keyCode = event.getIntegerValueField(.mouseEventButtonNumber)
                                                 ~^~~~~~~~~~~~~~~~~~~~~~
src/bin/MacKeyServer/main.swift:273:24: error: cannot find 'CGEventType' in scope
    } else if (type == CGEventType.tapDisabledByTimeout) {
                       ^~~~~~~~~~~
error: fatalError

I'm using Xcode 15.4(15F31d) and MacOS Sonoma 14.5

update: I added

import Cocoa
import CoreGraphics

at the beginning of the main.swift and the compilation and test succeeded.