PDA

View Full Version : Problem with keyDown/keyUp




swither
Feb 22, 2008, 12:10 PM
I've been trying to get my program to read the arrow keys and send commands over the serial RS232 interface. I've gotten everything working except the sensing the arrow key presses. This is what i have done:

I made a subclass out of NSWindow and changed the keyDown and keyUp methods to capture the arrow key presses. When keyDown is called, a corresponding boolean is set true and vice versa.

However, when i test the program out, nothing happens when i press the arrow keys, but when i press other keys the things that should have happen when i pressed the arrow keys happen. To test it i'm outputting the strings to a textview, and when i press other buttons after the arrow keys the strings from both the keydown and the keyup methods are outputted to the textview. Can anybody help me? Here's the code for one of the functions (they are very similar so...)

- (void)keyDown:(NSEvent *)theEvent {


if ([theEvent modifierFlags] & NSNumericPadKeyMask) { // mask för piltangenter
NSString *piltangenter = [theEvent charactersIgnoringModifiers];
unichar keyChar = 0;
if ( [piltangenter length] == 0 )
return; // döda tangenter
if ( [piltangenter length] == 1 ) {
keyChar = [piltangenter characterAtIndex:0];



if ( keyChar == NSLeftArrowFunctionKey ) {
left=YES;
return;
}
if ( keyChar == NSRightArrowFunctionKey ) {
right=YES;
return;
}
if ( keyChar == NSUpArrowFunctionKey ) {
up=YES;
return;
}
if ( keyChar == NSDownArrowFunctionKey ) {
down=YES;
return;
}

id mess;

mess = @"test\r\n";

if (up && !down && !left && !right) { //framåt
mess = @"fram\r\n";
}
else if (!up && down && !left && !right) { //bakåt
mess = @"bakåt\r\n";
}
else if (!up && !down && left && !right) { //vänster
mess = @"vänster\r\n";
}
else if (!up && !down && !left && right) { //höger
mess = @"höger\r\n";
}

[hover sendString: mess];

[super keyDown:theEvent];
}
}

[super keyDown:theEvent];
}



kainjow
Feb 22, 2008, 02:00 PM
What are you expecting it to do? You are calling return each time an arrow key is being pressed, so nothing else is happening in the code given.

phjo
Feb 22, 2008, 03:50 PM
My guess is the first responder is not the object whose method you pasted the code of, and that the events were trapped with the first responder (textview perhaps).

phjo

swither
Feb 23, 2008, 02:22 AM
What are you expecting it to do? You are calling return each time an arrow key is being pressed, so nothing else is happening in the code given.

hah, god, now i'm really embarassed, that was stupid of me. Thanks for spotting it!