Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can NodObjC Handle the Event Type? #32

Closed
matt- opened this issue Nov 22, 2013 · 3 comments
Closed

Can NodObjC Handle the Event Type? #32

matt- opened this issue Nov 22, 2013 · 3 comments

Comments

@matt-
Copy link

matt- commented Nov 22, 2013

I am trying to duplicate the following python:

# PyObjC-related imports
import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

KEY_UP = 11

class KeySocketApp(NSApplication):

    def sendEvent_(self, event):
        if event.type() is NSSystemDefined and event.subtype() is 8:
            data = event.data1()
            keyCode = (data & 0xFFFF0000) >> 16
            keyFlags = (data & 0x0000FFFF)
            keyState = (keyFlags & 0xFF00) >> 8
            keyRepeat = keyFlags & 0x1

            self.callback(keyCode, keyState)

        NSApplication.sendEvent_(self, event)


if __name__ == '__main__':


    def callback(code, state):
        if state is KEY_UP:
            print str(code)

    app = KeySocketApp.sharedApplication()
    app.callback = callback
    AppHelper.runEventLoop()

So far I have the following JS:

var $ = require('NodObjC')
$.import('Cocoa')

var pool = $.NSAutoreleasePool('alloc')('init'),
    app  = $.NSApplication('sharedApplication');

// set up the app delegate
var AppDelegate = $.NSObject.extend('AppDelegate')

AppDelegate.addMethod('sendEvent_:', 'v', function (self, event) {
  console.log(event);
  app.sendEvent_();
})

AppDelegate.register()

var delegate = AppDelegate('alloc')('init');
app('setDelegate', delegate);
app('activateIgnoringOtherApps', true);
app('run');
pool('release');
@trevorlinton
Copy link
Collaborator

@matt- Your example doesn't quite match up with the python one above (although I don't know python, they're doing two different things).

The Python example extends the NSApplication and overrides its sendEvent method (or, at least, appears to). While the nodobjc example you have creates a delegate and assigns the delegate to the application.

Delegates are not the same as extending (and overriding) a method in an existing object. What you'd probably want to do is:

$.NSApplication.extend('KeySocketApp');
$.KeySocketApp ... // and so on...

Delegates are just an object that NSApplication can ask questions about how it should function (what you implemented). However extending and overriding a method is taking the existing Class/Object and overriding its behavior to do something new. It's generally regarded (especially on NSApplication, a very core class/object) as a bad idea, but can be very powerful if you're careful.

Good luck!

@trevorlinton
Copy link
Collaborator

Not a bug, don't hate me for closing it :)

@matt-
Copy link
Author

matt- commented Aug 14, 2014

Oops, no hate! Sorry for not closing this myself I was able to resolve my issue with the following:

var $ = require('NodObjC');
$.framework('AppKit');

var app = $.NSApplication.extend('KeyApp');
app.addMethod('sendEvent:','v@:@', function(self, _cmd, nsevent){
    console.log(nsevent);
});
app.register();
app('sharedApplication')('run');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants