NSEvents

clapton541
Unregistered
 
Post: #1
How do you get multiple events from a keyDown event. I used the code
Code:
- (void) keyDown:(NSString *)keys
{
   unichar unicodeKey;
   int i = 0;
   unicodeKey = [ [ keys characters ] characterAtIndex:0 ];
   while(unicodeKey)
   {
           //all my functions here
   }
i++;
   unicodeKey = [ [ keys characters ] characterAtIndex:i ];
   }
}
I know it is sort of working because I can get one event but only one at a time, so I can't press more than one key in whatever im making. Also, how do I get the mouse position and then set the mouse position to somewhere else.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
That won't work. You can't really have a busy while loop that polls for events in Cocoa. The usual way to handle this is to set a variable on keyDown, act on it in a timer function, and reset it on keyUp.

This may also be of interest to you.
Quote this message in a reply
clapton541
Unregistered
 
Post: #3
So are you saying I should make a timer function to make an event function happen and have it not check for the key it checked last?
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #4
Simple example:
Code:
BOOL leftArrowKeyPressed = NO;

- (void) keyDown: (NSEvent *) event {
  if ([[event characters] characterAtIndex: 0] == NSLeftArrowFunctionKey) {
    leftArrowKeyPressed = YES;
  }
}

- (void) keyUp: (NSEvent *) event {
  if ([[event characters] characterAtIndex: 0] == NSLeftArrowFunctionKey) {
    leftArrowKeyPressed = NO;
  }
}

...

  timer = [NSTimer scheduledTimerWithTimeInterval: (1.0 / 120.0) target: self selector: @selector(timer:) userInfo: nil repeats: YES];

...

- (void) timer: (NSTimer *) timer {
  if (leftArrowKeyPressed) {
    moveLeft();
  }
}
Quote this message in a reply
clapton541
Unregistered
 
Post: #5
I see how that works, but how could I use it to get more than one key pressed at the same time.
Quote this message in a reply
clapton541
Unregistered
 
Post: #6
I don't exactly understand it, but I tried it and it works perfectly so thanks a lot.
Quote this message in a reply
clapton541
Unregistered
 
Post: #7
One last question, how do I set the mouse cursor to a certain position so that I could get the next position of the mouse like in a first person shooter game.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #8
CGWarpMousePosition (or something like that).

"When you dream, there are no rules..."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
On Mac OS X, you don't move the cursor, you read the raw mouse deltas out of the event, and possibly constrict the mouse pointer to the current window.
Quote this message in a reply
Post Reply