 |
 |
KeyEvent/keyListener in osX/objc?
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
I know that in pure java, I would use a KeyListner to supply key events if I want to control something in my app with keystrokes.
How's this done in objective C? I've been looking around the documentation and examples, and can't find it yet. Where should I be looking?
Thanks,
Nathaniel
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
I know what you mean by the KeyListener. I used that in Visual J++.
Cocoa uses a responder chain to handle mouse and keyboard events. If on object doesn't repond to the keyboard event, it moves on to the next object in the chain. The trick is to make your object the first responder.
I have no idea how to do this in Java. Try checking the documentation for "first responder."
------------------
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
I want to learn it in objective C....
What code would I use to call a method if the program receives a certain keystroke?
-Nathaniel
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
All the controls (NSTextField, NSButton, etc) are all subclasses of NSResponder, which provides the basic framework for dealing with events (including both mouse and keyboard). The easiest way to catch key events without destroying functionality is to subclass a control, and use the following:
- (void)keyDown  NSEvent *)aEvent
{
[super keyDown:aEvent]; // THIS CALLS THE ORIGINAL FUNCTIONALITY
// ADD YOUR OWN FUNCTIONALITY HERE
}
That will override the super-class's keydown event. But, since you call the super-class's keydown event anyways, you are simply adding your own functionality.
------------------
[This message has been edited by mr_sonicblue (edited 04-18-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
Ok, I must be majorly misunderstanding something....
Here's what I'm trying to do.... very simple, just a test to see if I can get events working right....
I have a window, with a textField..... I want it so that if I type any key on the keyboard, it prints the word "testing" in the textField.
So, I made a new project.... opened the nib file in interface builder. I dragged a textField to the window, made it uneditable.
I subclassed NStextField and called it MyTextField. I added an outlet called textField, and connected that to the textField in the window. THen I created the files.
in MyTextField.m I have the following:
#import "MyTextField.h"
@implementation MyTextField
- (void)keyDown  NSEvent *)aEvent
{
[super keyDown:aEvent]; // THIS CALLS THE ORIGINAL FUNCTIONALITY
[textField setStringValue: @"testing"];
}
@end
Unfortunately, all it does when I hit a key is beep. What major concept have I missed? What can I do to get it to work?
Thanks,
Nathaniel
[This message has been edited by robotic (edited 04-18-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
EDIT: OK, I'm really sorry. You can't subclass NSTextField and have this work. NSTextField uses an NSTextView to actually display/edit its text. NSTextField is just a simple frontend to an NSTextView. So, the only way I know how to catch key events in text fields is to not use text fields at all. Use text views instead. Sorry about the confusion. The below post is now correct.
You almost have it, but your logic is a little messed up (I'm not trying to diss you). This is how the responder chain is setup:
(first)
Key window's first responder (object with focus on key window)
Key window (window with focus...[NSApp keyWindow]) and its delegate
Main window's first responder (object with focus on main window)
Main window (You app's main window...[NSApp mainWindow]) and its delegate
NSApp (your app...NSApp) and its delegate
(last)
An event is sent from the top of the list, to the bottom, until one of the objects handles the event. Now, this is important depending on where you want to the keyboard event to be processed. If you want to detect it only when the text view has focus, you must subclass NSTextView and override its keyDown event. But, if you want it to be detected in a window, when there isn't a keyboard able object selected (like NSTextView), you must do this to NSWindow instead.
To subclass NSTextView:
- Subclass NSTextView in the Class tab of nib palette in Project Builder and create the files.
- Drag a CustomView from the views palette.
- Highlight the CustomView and open its info palette (COMMAND + SHIFT + I)
- In the info palette, switch to the custom class section (COMMAND + 5) and choose your subclass from the list.
Now your CustomView *is* of the custom class, so you don't have to make any outlets. Now, just modify you code so it reads like this:
- (void)keyDown  NSEvent *)aEvent
{
[super keyDown:aEvent]; // THIS CALLS THE ORIGINAL FUNCTIONALITY
[self setString: @"testing"]; // CALL METHOD IN SELF INSTEAD
}
For this to work, you make the text view the first responder by clicking it while the program is running.
EDIT: If you want to do some setup to your custom class (you can't do it in Interface Builder) implement the awake-from-nib method like this:
- (void)awakeFromNib
{
[self setBackgroundColor:[NSColor redColor]];
}
------------------
[This message has been edited by mr_sonicblue (edited 04-18-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
Great!
You've been more than helpful.
Thank you very much!
-Nathaniel
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
Originally posted by robotic:
Thank you very much!
You're surely welcome.
I'm just glad that's what you wanted
------------------
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|