1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Problem with keyDown/keyUp

Discussion in 'Mac Programming' started by swither, Feb 22, 2008.

  1. macrumors newbie

    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 ) {
                if ( keyChar == NSRightArrowFunctionKey ) {
                if ( keyChar == NSUpArrowFunctionKey ) {
                if ( keyChar == NSDownArrowFunctionKey ) {
    			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];
  2. Moderator emeritus


    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.
  3. macrumors regular

    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).

  4. macrumors newbie

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

Share This Page