NSWIndow KeyEvents
Hi,
I have one Java Application and running on Mac. I need some native interaction for keyboard inputs.
So through JNI, I'm interacting with objective C and Cocoa. I found my Java window in native code using below code.
NSWindow *window = [[NSApplication sharedApplication]keyWindow];
Above code returns my java app window. I confirmed this by printing title of the window.
From this, I need to implement key event for this NSWindow and I need to send the key values to Java application.
My question is, how to implement key events for this NSWindow?
I did subclass for NSWindow and overrides key events and casted NSWindow like below
MyNSWindow *window = (MyNSWIndow*)[[NSApplication sharedApplication]keyWindow];
But key events not firing.
Any idea about this?
Congratulations on getting the JNI to work and getting to the point that you have been able to obtain the window title. For sure, I don't know off hand how to finish the job. Several thoughts come to mind:
1) There is a Java Cocoa frameworks
http://developer.apple.com/legacy/mac/library/documentation/LegacyTechnologies/WebObjects/WebObjects_5/FoundationRef/Java/JavaFoundation.pdf
- although I think this is essentially give access to the Foundation classes and there's something called Java Native Foundation.
http://developer.apple.com/mac/library/documentation/CrossPlatform/Reference/JavaNativeFoundation_Functions/Reference/reference.html
- I think that enables access to Java data from Cocoa
2) How is your UI currently constructed? I did a Java project last year (on Windows) and the UI was constructed in SWING (or something) and I don't recall having to dive into the Win32 for anything.
3) I don't recall off-hand how keyboard events are sent to your window. To get mouse events, I've subclassed NSView to do this in the past. Your class mouseDown:(NSEvent*) method will be called appropriately. There's an example of this with here: http://bignerdranch.com/book/cocoa%C2%AE_programming_for_mac%C2%AE_os_x_3rd_edition
599 /Users/rmills/Projects/Solutions-Cocoa-3rd/18_MouseEvents/ImageFun $ grep mouse *.m
StretchView.m:- (void)mouseDown:(NSEvent *)event
StretchView.m:- (void)mouseDragged:(NSEvent *)e
StretchView.m:- (void)mouseUp:(NSEvent *)e
600 /Users/rmills/Projects/Solutions-Cocoa-3rd/18_MouseEvents/ImageFun $
I'm guessing that something similar can be used to find keystrokes in the UI.
4) If you could "strip down" the puzzle to the minimum and share that code with me, I'll be happy to spend some time this evening investigating this further (I live in California, so this evening is 10 hours from now.). You can upload the code here, or email it to me directly at robin@clanmills.com (please removed the build directory and zip up the rest - it should be 50k-100k).
So .... I don't know the answer to your question, however I'm confident that we can cooperate to solve this one quite quickly.
Thanks for your quick response.
Currently I don't have any problem with JNI interface.
My Java application is constructed using SWING.
My current problem is, I need to find out keystrokes for my java apps in native code and send it to Java.
There's a document here which describes call from Java (through JNI) to AppKit (See: Calling AppKit from AWT/Swing):
http://developer.apple.com/mac/library/technotes/tn2005/tn2147.html
I've modified the 18_MouseEvents code (which I mentioned above) and added:
- (void)keyDown:(NSEvent *)theEvent
{
NSLog(@"I see a key");
}
- (BOOL)acceptsFirstResponder
{
NSLog(@"acceptsFirstResponder");
return YES ;
}
- (BOOL) respondsToSelector:(SEL)aSelector
{
NSLog(@"respondsToSelector: %@",NSStringFromSelector(aSelector));
return [super respondsToSelector:aSelector];
}
- (void)mouseDown:(NSEvent *)event
bla bla bla ..... unmodified
in front of the mouseDown: method. Now my view (StretchView) is getting keystrokes. TN2147 discusses the underlying NSView in Java - and I feel we should be combine these to get what you want. I've added the respondsToSelector method to help debug/confuse you (it's optional, comment it off if you wish).
Once you have the keystrokes, TN2147 explains how to return them to Java.
On a different front, I googled up some Java keystroke code, and something like this is required to get key strokes, without dropping to JNI (which comes with cross-platform/installation implications/complications):
// Define ActionListener
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
InputMap inputMap = holdAll.getInputMap();
inputMap.put(enter, ACTION_KEY);
ActionMap actionMap = holdAll.getActionMap();
holdAll.setActionMap(actionMap);
My Java brains are rusty at the moment and I haven't been able to get the Java to work.
I know this still isn't the answer to your puzzle, however I hope you find it a useful stepping stone.
Thanks for your sample and info.
In the sample, all objects (NSWindow, NSView) are created by cocoa. So making subclass NSWindow and overriding events are becomes easy. But in my case, window is created by java application and I'm trying to capture keyevent of that window.
I created NSWindowDelegate and set that delegate as below to my NSWIndow (window from java) and it works fine.
[window setDelegate:myDelegate];
In the same way I'm looking to capture keyevent of the NSWindow.
Any idea???
Is it possible to send me a simple example of this and I'll fix it for you. At the moment, my Java's a bit rusty and although I can imagine what you're describing, I don't have code with which to reproduce your circumstances.
I'm very happy to help you. However you'll have to help me to help you, and that requires some code that we have in common. When you reduce the puzzle to its simplest, three great things happen. Firstly, it helps clear your thoughts. Secondly, I start at exactly the point where you are stuck. Thirdly, we have code in common to discuss how/why/ways to fix it.
Thanks for your support.
At this point, I'm unable to send code. I will try to write small sample program and send it to you later this week.