Julian Ceipek - 2011-05-19

I wrote some proof of concept coe for mapping the analog Nunchuck joystick to keys (my goal was to play Portal 2, but the mouselook won't work). The code is very rough and is only intended as a demonstration of functionality. I don't know if this project is still alive, but maybe one of the actual developers can implement this properly in the near future.

#ifdef NUNCHUK_JOY_TO_KEYS
                static NSTimer *rightTimer;
                static NSTimer *leftTimer;
                static NSTimer *upTimer;
                static NSTimer *downTimer;
                static BOOL rightActive = FALSE;
                static BOOL leftActive = FALSE;
                static BOOL upActive = FALSE;
                static BOOL downActive = FALSE;
                SEL selector = @selector(sendKeyboardEvent:keyDown:);
        
                if (scaledX > 0.5) {
                    NSLog(@"Press D");
                    if (rightActive == FALSE)
                    {                        
                        //Prepare the invocation to repeat
                        NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
                        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                        [invocation setSelector:selector];
                        
                        //Prepare the values of the arguments
                        NSInteger key = 2;
                        BOOL down = TRUE;
                        
                        //Set the arguments
                        [invocation setTarget:self];
                        [invocation setArgument:&key atIndex:2];
                        [invocation setArgument:&down atIndex:3];
                        
                        rightTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                       invocation:invocation
                                                        repeats:YES];
                        rightActive = TRUE;
                    }
                    
                } else if (rightActive == TRUE) {
                    [rightTimer invalidate];
                    [self sendKeyboardEvent:2 keyDown:FALSE];
                    rightActive = FALSE;
                }
        
                if (scaledX < -0.5) {
                    NSLog(@"Press A");
                    if (leftActive == FALSE)
                    {                        
                        //Prepare the invocation to repeat
                        NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
                        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                        [invocation setSelector:selector];
                        
                        //Prepare the values of the arguments
                        NSInteger key = 0;
                        BOOL down = TRUE;
                        
                        //Set the arguments
                        [invocation setTarget:self];
                        [invocation setArgument:&key atIndex:2];
                        [invocation setArgument:&down atIndex:3];
                        
                        leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                                  invocation:invocation
                                                                     repeats:YES];
                        leftActive = TRUE;
                    }
                    
                } else if (leftActive == TRUE) {
                    [leftTimer invalidate];
                    [self sendKeyboardEvent:0 keyDown:FALSE];
                    leftActive = FALSE;
                }
        
                if (scaledY > 0.5) {
                    NSLog(@"Press W");
                    if (upActive == FALSE)
                    {                        
                        //Prepare the invocation to repeat
                        NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
                        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                        [invocation setSelector:selector];
                        
                        //Prepare the values of the arguments
                        NSInteger key = 13;
                        BOOL down = TRUE;
                        
                        //Set the arguments
                        [invocation setTarget:self];
                        [invocation setArgument:&key atIndex:2];
                        [invocation setArgument:&down atIndex:3];
                        
                        upTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                                 invocation:invocation
                                                                    repeats:YES];
                        upActive = TRUE;
                    }
                    
                } else if (upActive == TRUE) {
                    [upTimer invalidate];
                    [self sendKeyboardEvent:13 keyDown:FALSE];
                    upActive = FALSE;
                }
        
                if (scaledY < -0.5) {
                    NSLog(@"Press S");
                    if (downActive == FALSE)
                    {                        
                        //Prepare the invocation to repeat
                        NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
                        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                        [invocation setSelector:selector];
                        
                        //Prepare the values of the arguments
                        NSInteger key = 1;
                        BOOL down = TRUE;
                        
                        //Set the arguments
                        [invocation setTarget:self];
                        [invocation setArgument:&key atIndex:2];
                        [invocation setArgument:&down atIndex:3];
                        
                        downTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                               invocation:invocation
                                                                  repeats:YES];
                        downActive = TRUE;
                    }
                    
                } else if (downActive == TRUE) {
                    [downTimer invalidate];
                    [self sendKeyboardEvent:1 keyDown:FALSE];
                    downActive = FALSE;
                }
        
            #endif

To use this, simply insert it into the - (void) joyStickChanged:(WiiJoyStickType)type tiltX:(unsigned short)tiltX tiltY:(unsigned short)tiltY {} method in the AppController.m file, at the end. Obviously, it relies on

#define NUNCHUK_JOY_TO_KEYS 1

at the top of the file.

I have noticed that enabling ir mouse control slows down recognition significantly. Maybe a time delay is causing an issue somewhere.