#import "HotkeyPanel.h"
#import "HotkeyApp.h"

@implementation HotkeyPanel

-(void)keyDown:(NSEvent *)theEvent{
    unichar keyChar=[[theEvent charactersIgnoringModifiers] characterAtIndex:0];
    
    //NSLog(@"%c,%d",keyChar,keyChar);
    [_keyStr release];
    if(keyChar==NSUpArrowFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Up"];
    else if(keyChar==NSDownArrowFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Down"];
    else if(keyChar==NSLeftArrowFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Left"];
    else if(keyChar==NSRightArrowFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Right"];
    else if(keyChar==NSF1FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F1"];
    else if(keyChar==NSF2FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F2"];
    else if(keyChar==NSF3FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F3"];
    else if(keyChar==NSF4FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F4"];
    else if(keyChar==NSF5FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F5"];
    else if(keyChar==NSF6FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F6"];
    else if(keyChar==NSF7FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F7"];
    else if(keyChar==NSF8FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F8"];
    else if(keyChar==NSF9FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F9"];
    else if(keyChar==NSF10FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F10"];
    else if(keyChar==NSF11FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F11"];
    else if(keyChar==NSF12FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F12"];
    else if(keyChar==NSF13FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F13"];
    else if(keyChar==NSF14FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F14"];
    else if(keyChar==NSF15FunctionKey) _keyStr=[[NSString alloc] initWithString:@"F15"];
    else if(keyChar==NSDeleteFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Del"];
    else if(keyChar==NSHomeFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Home"];
    else if(keyChar==NSEndFunctionKey) _keyStr=[[NSString alloc] initWithString:@"End"];
    else if(keyChar==NSPageUpFunctionKey) _keyStr=[[NSString alloc] initWithString:@"PgUp"];
    else if(keyChar==NSPageDownFunctionKey) _keyStr=[[NSString alloc] initWithString:@"PgDn"];
    else if(keyChar==NSClearLineFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Clr"];
    else if(keyChar==NSHelpFunctionKey) _keyStr=[[NSString alloc] initWithString:@"Hlp"];
    else if(keyChar==' ') _keyStr=[[NSString alloc] initWithString:@"Space"];
    else if(keyChar==13) _keyStr=[[NSString alloc] initWithString:@"Ret"];
    else if(keyChar==3) _keyStr=[[NSString alloc] initWithString:@"Enter"];
    else if(keyChar==9) _keyStr=[[NSString alloc] initWithString:@"tab"];
    else if(keyChar==27) _keyStr=[[NSString alloc] initWithString:@"esc"];
    else _keyStr=[[NSString alloc] initWithCharacters:&keyChar length:1];
    _keyCode=[theEvent keyCode];
    //NSLog(@"keyDown : %x (char)",keyChar);
    //NSLog(@"keyDown : %x (code)",_keyCode);
    //NSLog(@"keyDown : %a (String)",_keyStr);
    [[HotkeyApp sharedApplication] endSheet:self returnCode:1];
    [self close];
}

-(unsigned short)keyCode{
    return _keyCode;
}

-(NSString *)keyStr{
    return _keyStr;
}

-(void)dealloc{
    [_keyStr release];
    [super dealloc];
}

@end
