2

I am making a simple piano game for Mac. And when user pressed the keyboard,the key of piano

can be pressed simultaneously.

However, I am clueless about how to check when "D,F....." key is pressed on the Mac's keyboard.

Objective-C

Skye Wood
  • 21
  • 1
  • 3
  • possible duplicate of [Simulating key press events in Mac OS X](http://stackoverflow.com/questions/2379867/simulating-key-press-events-in-mac-os-x) – Chris Ledet Jul 06 '12 at 03:18
  • 2
    This doesn't seem like a duplicate of that question at all. Skye seems to be asking about _detecting_ key presses, not generating them. – Andrew Madsen Jul 06 '12 at 03:31

3 Answers3

2

As with so many things, there are multiple ways to do this. However a simple way is to override -keyDown: in your NSView subclass. Presumably, this would be the NSView subclass that draws your piano keyboard. Example:

- (void)keyDown:(NSEvent *)event
{
    switch ([event keyCode])
    {
        case 0x02:
            // D key pressed
            break;
        case 0x03:
            // F key pressed
            break;
        // etc.
    }
}

I find the Key Codes app handy for finding key codes, but you can also just put a log statement in your -keyDown: method then press keys to find the corresponding codes. They're also in the <HIToolbox/Events.h> header.

See Apple's Event Handling Guide for more information.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • Thanks,But I'm new to this.Where and how should I override - keydown: Now,I draw the piano in a viewController.Is that right? Thank you. – Skye Wood Jul 06 '12 at 08:09
  • You override `-keyDown:` in the subclass of NSView you use to draw the piano keys. In order to actually receive key events, the view will have to be the first responder. You can cause the view to become first responder by calling `[window makeFirstResponder:pianoView]` on the window containing the view. I added a link to the relevant documentation to my answer. You should read that. – Andrew Madsen Jul 06 '12 at 14:24
1

Do you have a NSView subclass as the piano view? If you do, then just override the -(void)keyDown:(NSEvent *)event method, and record down whatever key you want. For Example:

-(void)keyDown:(NSEvent *)event {
    NSString *characters;
    characters = [event characters];

    switch (characters)
    {
        case 'd':
             //do something;
        default:
             break;
     }
}
Rob Evans
  • 6,750
  • 4
  • 39
  • 56
TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • What do you use to display the piano then? – TheAmateurProgrammer Jul 06 '12 at 08:53
  • I add this method to the NSView subclass. But can't check for keyboard events. I'm new to this,maybe I use this method a wrong way.If you can give me a demo. I'll appreciate it. – Skye Wood Jul 06 '12 at 12:38
  • Does the keyDown method only work in textView or textField? I found a demo but only works in textField.I display the piano in a viewController,then when I tab the keyboard,nothing happened. – Skye Wood Jul 06 '12 at 13:16
  • keyDown: works in every NSView. If you're using a viewController, then do you have a NSView subclass to display? If you don't, it's highly recommended that you subclass it for drawing purposes, then you can override keyDown: Also, as @Andrew Madsen said, you would need to set the view as the First Responder in order to actually receive key events. – TheAmateurProgrammer Jul 07 '12 at 01:35
  • Solve it finally.Thank you very much! – Skye Wood Jul 07 '12 at 06:24
0

By using this code you can catch all key press events.

i is KeyCode . sample kVK_End

while (true)

{

    for (int i=0; i<128; i++)
    {
        if (CGEventSourceKeyState(kCGEventSourceStateCombinedSessionState,i))
        {
            NSLog(@"Key Press");
        }
    }
}
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
Mahdi Nili
  • 27
  • 2