Problem with keyDown/keyUp

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

  1. macrumors newbie

    Joined:
    Jun 13, 2007
    #1
    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];
    }
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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

    Joined:
    Jan 8, 2008
    #3
    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
     
  4. thread starter macrumors newbie

    Joined:
    Jun 13, 2007
    #4
    hah, god, now i'm really embarassed, that was stupid of me. Thanks for spotting it!
     

Share This Page