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...)
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...)
Code:
- (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];
}