# Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

## #1 2017-05-31 23:41:41

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

### key combos

Hey all, just looking for a way to perform a key combo.

I have a program that when A is pushed causes the character to jump. However what I want it to do next is then work out whether or not the LEFT or RIGHT keys are also pressed and jump in that direction.

Here's what I was trying to do:

if (joypad == J_LEFT){
//do jump logic here
}
else if (joypad == J_LEFT){
//do jump logic here
}
else{
//do jump logic here
}
}

however this dosn't seem to work, any solutions?

Offline

## #2 2017-06-01 00:10:27

DonaldHays
Member
From: Seattle
Registered: 2016-08-01
Posts: 35
Website

### Re: key combos

Hi there!

`joypad` returns a byte, with bits for the different buttons combined together via bitwise OR. For example, A is 00010000, and Left is 00000010. If the user is pressing both A and Left, the result will be 00010010. Checking that for equality against just A or just Left will return false, because the combined value (00010010) is not equal to the individual values (00010000 and 00000010).

What you should do with these types of values instead is use the bitwise AND operator to see if the resulting value contains the things you are looking for. The combined value (00010010) AND A (00010000) is A (00010000). The combined value (00010010) AND, say, Select (01000000) is 0 (00000000). Now, in C, logically 0 is false and all non-zero values are true, so (value AND button) is logically equivalent to "true if button is in value, false otherwise".

With that preamble out of the way, what you want is something like this.

#### Code:

if(joypadValue & J_A) {
// We know that the A button is down, so we're going to jump

if(joypadValue & J_LEFT) {
// Jump left
} else if(joypadValue & J_RIGHT) {
// Jump right
} else {
// Jump straight up and down
}
}

Last edited by DonaldHays (2017-06-01 00:13:05)

Offline

## #3 2017-06-01 01:44:15

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

### Re: key combos

Thanks, this is just what I was looking for.

Offline