SourceForge has been redesigned. Learn more.
Close

Code to map analog sticks to keys

2008-06-08
2013-05-23
  • Nathan Sanders

    Nathan Sanders - 2008-06-08

    This weekend I hacked up some code to map the various analog sticks  to the arrow keys. It works pretty well for games that expect digital input. However, because it currently uses the same scale for both axes, diagonals can be a little tricky to get sometimes. I haven't tried it on anything that expects analog input natively, like N64 games, but it's probably pretty disappointing there.

    The changes are pretty simple right now because everything is hard-coded. A lot more work is needed to hook into the interface and make everything fully customisable. I don't really know Objective-C, and I'm lazy, so it may take me a while to fix these omissions.

    Here is a svn diff of AppController.h and AppController.m in case you want to try it. Start from the subversion trunk.

    Index: AppController.h

    --- AppController.h    (revision 209)
    +++ AppController.h    (working copy)
    @@ -110,6 +110,9 @@
         WiiRemote* wii;
         CGPoint point;
         //BOOL isPressedBButton, isPressedAButton, isPressedHomeButton, isPressedUpButton, isPressedDownButton, isPressedLeftButton, isPressedRightButton, isPressedOneButton, isPressedTwoButton, isPressedPlusButton, isPressedMinusButton;
    +    BOOL isTiltedXNunchukJoyStick, isTiltedYNunchukJoyStick,
    +         isTiltedXClassicControllerLeftJoyStick, isTiltedYClassicControllerLeftJoyStick,
    +         isTiltedXClassicControllerRightJoyStick, isTiltedYClassicControllerRightJoyStick;
        
         BOOL isLeftButtonDown, isRightButtonDown;

    Index: AppController.m

    --- AppController.m    (revision 209)
    +++ AppController.m    (working copy)
    @@ -985,31 +985,153 @@
         details.
    */
    - (void) joyStickChanged:(WiiJoyStickType)type tiltX:(unsigned short)tiltX tiltY:(unsigned short)tiltY {
    -
    +    float shiftedX;
    +    float shiftedY;
    +    float scaledX;
    +    float scaledY;
    +    unsigned short max;
    +    unsigned short center;
         if (type == WiiNunchukJoyStick) {
    -            unsigned short max = 0xE0;
    -            unsigned short center = 0x80;
    +        max = 0xE0;
    +        center = 0x80;
            
    -            float shiftedX = (tiltX * 1.0) - (center * 1.0);
    -            float shiftedY = (tiltY * 1.0) - (center * 1.0);
    +        shiftedX = (tiltX * 1.0) - (center * 1.0);
    +        shiftedY = (tiltY * 1.0) - (center * 1.0);
            
    -            float scaledX = (shiftedX * 1.0) / ((max - center) * 1.0);
    -            float scaledY = (shiftedY * 1.0) / ((max - center) * 1.0);
    +        scaledX = (shiftedX * 1.0) / ((max - center) * 1.0);
    +        scaledY = (shiftedY * 1.0) / ((max - center) * 1.0);
            
    -            // NSLog(@"Joystick X = %f  Y= %f", scaledX, scaledY);
    -            [joystickQCView setValue:[NSNumber numberWithFloat: scaledX] forInputKey:[NSString stringWithString:@"X_Position"]];
    -            [joystickQCView setValue:[NSNumber numberWithFloat: scaledY] forInputKey:[NSString stringWithString:@"Y_Position"]];
    +        // NSLog(@"Joystick X = %f  Y= %f", scaledX, scaledY);
    +        [joystickQCView setValue:[NSNumber numberWithFloat: scaledX] forInputKey:[NSString stringWithString:@"X_Position"]];
    +        [joystickQCView setValue:[NSNumber numberWithFloat: scaledY] forInputKey:[NSString stringWithString:@"Y_Position"]];
            
    -            [joystickX setStringValue: [NSString stringWithFormat:@"%00X", tiltX]];       
    -            [joystickY setStringValue: [NSString stringWithFormat:@"%00X", tiltY]];       
    +        [joystickX setStringValue: [NSString stringWithFormat:@"%00X", tiltX]];       
    +        [joystickY setStringValue: [NSString stringWithFormat:@"%00X", tiltY]];       
    +        if(scaledX < -0.7) {
    +            if(!isTiltedXNunchukJoyStick) {
    +                //[self sendModifierKeys:map isPressed:YES];
    +                [self sendKeyboardEvent:123 keyDown:YES];
    +                isTiltedXNunchukJoyStick = YES;
    +            }
    +        } else if(scaledX > 0.7) {
    +            if(!isTiltedXNunchukJoyStick) {
    +                [self sendKeyboardEvent:124 keyDown:YES];
    +                isTiltedXNunchukJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedXNunchukJoyStick) {
    +                //[self sendModifierKeys:map isPressed:NO];
    +                [self sendKeyboardEvent:123 keyDown:NO];
    +                [self sendKeyboardEvent:124 keyDown:NO];
    +                isTiltedXNunchukJoyStick = NO;
    +            }
    +        if(scaledY < -0.6) {
    +            if(!isTiltedYNunchukJoyStick) {
    +                //[self sendModifierKeys:map isPressed:isPressed];
    +                [self sendKeyboardEvent:125 keyDown:YES];
    +                isTiltedYNunchukJoyStick = YES;
    +            }
    +        } else if(scaledY > 0.7) {
    +            if(!isTiltedYNunchukJoyStick) {
    +                [self sendKeyboardEvent:126 keyDown:YES];
    +                isTiltedYNunchukJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedYNunchukJoyStick) {
    +                [self sendKeyboardEvent:125 keyDown:NO];
    +                [self sendKeyboardEvent:126 keyDown:NO];
    +                isTiltedYNunchukJoyStick = NO;
    +            }
         } else if (type == WiiClassicControllerLeftJoyStick) {
    -   
    -   
    +        max = 0x38;
    +        center = 0x20;
    +       
    +        shiftedX = (tiltX * 1.0) - (center * 1.0);
    +        shiftedY = (tiltY * 1.0) - (center * 1.0);
    +       
    +        scaledX = (shiftedX * 1.0) / ((max - center) * 1.0);
    +        scaledY = (shiftedY * 1.0) / ((max - center) * 1.0);
    +        if(scaledX < -0.7) {
    +            if(!isTiltedXClassicControllerLeftJoyStick) {
    +                //[self sendModifierKeys:map isPressed:YES];
    +                [self sendKeyboardEvent:123 keyDown:YES];
    +                isTiltedXClassicControllerLeftJoyStick = YES;
    +            }
    +        } else if(scaledX > 0.7) {
    +            if(!isTiltedXClassicControllerLeftJoyStick) {
    +                [self sendKeyboardEvent:124 keyDown:YES];
    +                isTiltedXClassicControllerLeftJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedXClassicControllerLeftJoyStick) {
    +                //[self sendModifierKeys:map isPressed:NO];
    +                [self sendKeyboardEvent:123 keyDown:NO];
    +                [self sendKeyboardEvent:124 keyDown:NO];
    +                isTiltedXClassicControllerLeftJoyStick = NO;
    +            }
    +        if(scaledY < -0.6) {
    +            if(!isTiltedYClassicControllerLeftJoyStick) {
    +                //[self sendModifierKeys:map isPressed:isPressed];
    +                [self sendKeyboardEvent:125 keyDown:YES];
    +                isTiltedYClassicControllerLeftJoyStick = YES;
    +            }
    +        } else if(scaledY > 0.7) {
    +            if(!isTiltedYClassicControllerLeftJoyStick) {
    +                [self sendKeyboardEvent:126 keyDown:YES];
    +                isTiltedYClassicControllerLeftJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedYClassicControllerLeftJoyStick) {
    +                [self sendKeyboardEvent:125 keyDown:NO];
    +                [self sendKeyboardEvent:126 keyDown:NO];
    +                isTiltedYClassicControllerLeftJoyStick = NO;
    +            }
                 [ccLeftX setStringValue: [NSString stringWithFormat:@"%00X", tiltX]];
                 [ccLeftY setStringValue: [NSString stringWithFormat:@"%00X", tiltY]];
         } else if (type == WiiClassicControllerRightJoyStick) {
    -   
    -   
    +        max = 0x1B;
    +        center = 0x0F;
    +       
    +        shiftedX = (tiltX * 1.0) - (center * 1.0);
    +        shiftedY = (tiltY * 1.0) - (center * 1.0);
    +       
    +        scaledX = (shiftedX * 1.0) / ((max - center) * 1.0);
    +        scaledY = (shiftedY * 1.0) / ((max - center) * 1.0);
    +        if(scaledX < -0.7) {
    +            if(!isTiltedXClassicControllerRightJoyStick) {
    +                //[self sendModifierKeys:map isPressed:YES];
    +                [self sendKeyboardEvent:123 keyDown:YES];
    +                isTiltedXClassicControllerRightJoyStick = YES;
    +            }
    +        } else if(scaledX > 0.7) {
    +            if(!isTiltedXClassicControllerRightJoyStick) {
    +                [self sendKeyboardEvent:124 keyDown:YES];
    +                isTiltedXClassicControllerRightJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedXClassicControllerRightJoyStick) {
    +                //[self sendModifierKeys:map isPressed:NO];
    +                [self sendKeyboardEvent:123 keyDown:NO];
    +                [self sendKeyboardEvent:124 keyDown:NO];
    +                isTiltedXClassicControllerRightJoyStick = NO;
    +            }
    +        if(scaledY < -0.6) {
    +            if(!isTiltedYClassicControllerRightJoyStick) {
    +                //[self sendModifierKeys:map isPressed:isPressed];
    +                [self sendKeyboardEvent:125 keyDown:YES];
    +                isTiltedYClassicControllerRightJoyStick = YES;
    +            }
    +        } else if(scaledY > 0.7) {
    +            if(!isTiltedYClassicControllerRightJoyStick) {
    +                [self sendKeyboardEvent:126 keyDown:YES];
    +                isTiltedYClassicControllerRightJoyStick = YES;
    +            }
    +        } else
    +            if(isTiltedYClassicControllerRightJoyStick) {
    +                [self sendKeyboardEvent:125 keyDown:NO];
    +                [self sendKeyboardEvent:126 keyDown:NO];
    +                isTiltedYClassicControllerRightJoyStick = NO;
    +            }   
                 [ccRightX setStringValue: [NSString stringWithFormat:@"%00X", tiltX]];
                 [ccRightY setStringValue: [NSString stringWithFormat:@"%00X", tiltY]];   
         }

     
    • Kris

      Kris - 2009-07-18

      Any advice or links on how to build this project?

       
    • Kris

      Kris - 2009-07-20

      Any advice or links on how to build this project?

       
      • Bjørn-Olav Strand

        What happens when you press build in xcode?

         

Log in to post a comment.