Multiple keys held in Cocoa (OS X)

I am new to Mac programming, so I hope this is not obvious.

In short, I don't seem to get the few keys pressed. I created this snippet that never works, and therefore never prints multiple keys. However, I get single prints.

- (void)keyDown:(NSEvent *) theEvent {
    NSString *characters = [theEvent characters];
    assert([characters length]<2);
    for (int i=0;i<[characters length];++i) {
      NSLog(@"k=[%d]\n", [characters characterAtIndex:i]);
    }
  }

Does anyone know what I'm doing wrong? In case you are interested, I need a few keystrokes for the OpenGL viewer application. Perhaps I'm just completely unfamiliar with such an application.

Edit: after further research, I found the following: http://www.cocoadev.com/index.pl?GameKeyBoardHandling This makes sense, based on the discussion here, in that it repeats only the last key. When the keyDown event is selected, the down key is placed in the set and deleted on keyUp. This means that the set has a full set of held keys, which avoids the problem of a single repetition. This is good enough, so I am using this method now. It seems to work well, and since it uses a standard keyboard event system (not HID), there should be no compatibility issues.

Regards, Shane

+3
source share
5 answers

NSEvent : . , . , .

characters : . , , . , ; . - modifierFlags.

, DDHIDLib HID API Leopard . , , , , .

, , Dvorak. , , . Flash , QWERTY, (, Dvorak). , QWERTY; QWERTY.

+2

, , 1. - IME .

, . , keyDown:. isARepeat NO, YES.

, keyDown: keyUp:. , f g, keyDown keyDown: f keyDown: g. , f, g, keyUp: f, keyDown: g. , , .

, , , , .

+1

, , , :

#import <Carbon/Carbon.h> // for kVK_* names

- (void)keyDown:(NSEvent *event)
{
    unsigned keyCode = [event keyCode];
    if (keyCode < 128) keysDown[keyCode] = YES;
}

- (void)keyUp:(NSEvent *event)
{
    unsigned keyCode = [event keyCode];
    if (keyCode < 128) keysDown[keyCode] = NO;
}

- (void)whereverYouUpdateTheStateOfYourGame
{
    if (keysDown[kVK_ANSI_A] && !keysDown[kVK_ANSI_D]) { /* move left */ }
    if (keysDown[kVK_ANSI_D] && !keysDown[kVK_ANSI_A]) { /* move right */ }
    if (keysDown[kVK_ANSI_W] && !keysDown[kVK_ANSI_S]) { /* move up */ }
    if (keysDown[kVK_ANSI_S] && !keysDown[kVK_ANSI_W]) { /* move down */ }
}

, .

+1

, . keyDown ( , ) ; Apple NSEvent. , UNICODE, , NSString UNICODE, , , .

- . -, - :

if ([event modifiers] & NSShiftKeyMask) {
    ...
}
0

Source: https://habr.com/ru/post/1713020/


All Articles