• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Simple input question...

## Recommended Posts

The input on the current platform that I am learning to program on (GBA) is stored in a 16bit register (only uses 9 bits to correspond to each button). Ok. So the bit is either on or off. This is fine for checking if a button is currently being pressed. But I want to have another 16bit variable that just trackes when a button was first pressed. This means that a bit would only be on during the first frame of it being pressed. For example: ------------------ frame 1: GBAInput: 0000000000000000 JustPressed: 0000000000000000 ------------------ frame 2: GBAInput: 0000000010000000 JustPressed: 0000000010000000 ------------------ frame 3: GBAInput: 0000000010000000 JustPressed: 0000000000000000 ------------------ frame 4: GBAInput: 0000000010000000 JustPressed: 0000000000000000 ------------------ I know this is possible, but how do I do it? I was thinking of using another 16bit variable to store the previous frames gba input, and then XOR it with the current input to give me my justPressed input, but i''m not sure if that would work. Any suggestions?

##### Share on other sites
You''re on the right track with the XOR, but there is a small problem that has to be sorted out. Using XOR with the state of the last frame''s input and the current frames input will give you flags for all buttons that HAVE CHANGED , not just the presses but the releases also.

This can be solved using an additional bitwise operation. If the last state was zero, then the current state MUST be 1 for a button press. In that case, just use the & operator with the current state and the has changed value, ie

hasChanged = lastState ^ currentState;
wasPressed = hasChanged & currentState;

  /* The current input state of the buttons  */u16 g_CurrentInputState = 0x0000;/* The input state of the buttons on the last frame */u16 g_JustPressedState    = 0x0000;/* Function to update the input  */void UpdateInput(void){	u16 LastInputState = g_CurrentInputState;	GetInputState(&g_CurrentInputState);	g_JustPressedState = g_CurrentInputState &(g_CurrentInputState ^ LastInputState);}

• ### Forum Statistics

• Total Topics
628293
• Total Posts
2981869

• 11
• 10
• 10
• 11
• 17