Removing delay until key repeat

hyperutila
Unregistered
 
Post: #1
I just finished a duplicate of asteroids in Carbon using xcode, but there's one problem. The event handler uses the system preference for delay before key repeats for the kEventRawKeyRepeat event parameter. How do you specify the delay yourself? I don't want there to be any delay at all. What happens now is that if you hold down the left arrowkey(for example), your ship will turn counterclockwise once(with the kEventRawKeyDown parameter), and then wait for the length of the system's delay, then proceed with executing the kEventRawKeyRepeat parameter. How do I get it to jump immediately into the key repeat? By the way, you can set the delay before key repeat in system preferences under keyboard and mouse preference. Altering this preference affects my program.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #2
You shouldn't be using kEventRawKeyRepeat at all. Just set the state the ship to something like "spin_clockwise" on a keyDown and revert it to "spin_none" on a keyUp. Then, on each cycle, the ship spins a predetermined amount according to the current state.
This is a bit of the key down from my current game:

Code:
        switch( keyCode )
        {
        case 123:    // Arrow Left
            playerUnit.motionRequest = kMotionRollLeft;
            break;

        case 124:    // Arrow Right
            playerUnit.motionRequest = kMotionRollRight;
            break;

        case 125:    // Arrow Down
            playerUnit.motionRequest = kMotionClimb;
            break;

        case 126:    // Arrow Up
            playerUnit.motionRequest = kMotionDive;
            break;

        case 35:    // p
            ResumeGame();
            break;
        }
Quote this message in a reply
hyperutila
Unregistered
 
Post: #3
Wow! That's smart. So basically, when I receive a kEventRawKeyDown telling that the arrowkey is down, I set a variable indicating rotation that is handled in an event timer, and when I receive a kEventRawKeyUp, I set the variable to cancel rotation. That should work. Thanks.
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #4
you should set it to cancel rotation because of small discrepancies. Such as if I press a to turn left, and then I quickly press d to turn right, but for a moment my hand is still on a. So lets look at that:

a down:
rotate left
d down:
rotate right (aka don't rotate at all (rotations should cancel each other out))
a up:
cancel rotations (ack now you aren't rotating at all)

I suggest:
(assumes +degrees = counter clockwise and negative degrees is counter clockwise)
a down:
add 10 to degrees per second rotated
d down:
subtract 10 from degrees per second rotated
a up:
subtract 10 from degrees per second rotated
and eventually d up:
add 10 to degrees per second rotated

see how that works?
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #5
Well, I posted a simplified form of doing this. If you want your code to be able to account for two arrows pressed at the same time, it should keep track of the state of each arrow button as well as the last key pressed, and then account for combinations. So, in a key_down event, you would have to run something like (pseudocode):

Code:
if ( arrow_up_isdown && arrow_right_isdown )
{
    ship.move_up_and_right();
    return;
}

Then, resolve the cancelling of the motion in the key_up event.

I also suggest, as Skyhawk pointed out, that you work with time-based rotations, so the rotation would look something like:

Code:
ship.yaw_angle += ( max_turn_rate_right * delta_time );

where delta_time is the time in seconds (or ticks, depending on how you want to do things) passed since the last cycle.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  unexpexced jump of graphics and delay displaying sefiroths 4 5,115 Sep 19, 2011 12:27 AM
Last Post: sefiroths
  Removing Vector Items In Loop? Nick 5 2,987 Oct 21, 2006 06:20 PM
Last Post: Nick