Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

swither

macrumors newbie
Original poster
Jun 13, 2007
29
0
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...)

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];
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors regular
Jan 8, 2008
149
1
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

macrumors newbie
Original poster
Jun 13, 2007
29
0
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.