GLFW_KEY_W not working (not compiling


#1

Hello, I recently added this code:

case GLFW_KEY_W: y_location+=5; break; case GLFW_KEY_S: y_location-=5; break; case GLFW_KEY_A: x_location-=5; break; case GLFW_KEY_87: x_location+=5; break;

Unfortunately, it does not compile. It keeps saying [quote]In function ‘void handleKeypress(int, int)’:expressionless:
|23|error: ‘GLFW_KEY_W’ was not declared in this scope|
|26|error: ‘GLFW_KEY_S’ was not declared in this scope|
|29|error: ‘GLFW_KEY_A’ was not declared in this scope|
|32|error: ‘GLFW_KEY_D’ was not declared in this scope|
[/quote]

So, what I am doing wrong this time? Thanks for the effort.


#2

It looks like you haven’t included the glfw header file.


#3

But it works with a lot of other keys. And the glfw header is included.


#4

It’s difficult to help without seeing more of the code, however there is at least one error in what you’ve put above as there is no GLFW_KEY_87, I think you mean GLFW_KEY_W which has number 87. Note that for the printable keys you can also use their upper case character representation, such as'W' in this case.


#5

Well, let’s see if this code might be of some help:

void GLFWCALL handleKeypress (int key, int press)  {
   switch (key) {
      case GLFW_KEY_ESC:
         exit(1);
         break;
      case GLFW_KEY_RIGHT:
         rotate_y+=5;
         break;
      case GLFW_KEY_LEFT:
         rotate_y-=5;
         break;
      case GLFW_KEY_UP:
         rotate_x+=5;
         break;
      case GLFW_KEY_DOWN:
         rotate_x-=5;
         break;
      case GLFW_KEY_W:
         y_location+=5;
         break;
      case GLFW_KEY_S:
         y_location-=5;
         break;
      case GLFW_KEY_A:
         x_location-=5;
         break;
      case GLFW_KEY_D:
         x_location+=5;
         break;
    }
}

Such code will simply not compile unless I comment out everything between

with messages such as


#6

This helps a lot actually, since you are using GLFW_KEY_ESC which only exists in GLFW 2, not GLFW 3 (GLFW 3 has GLFW_KEY_ESCAPE).

So it looks like you are using GLFW 2 not GLFW 3. GLFW 2 does not have the 0-9 and A-Z keys defined using GLFW_KEY_? so you need to using 'W', 'S', 'A', 'D' or switch to GLFW 3. If you switch do read http://www.glfw.org/docs/latest/moving_guide.html#moving_keys.


#7

So do I have to know the corresponding ascii numbers of these keys? I ask that because the “key” bariable is an int. Or shall I change it to a char and use [quote]case ‘w’[/quote]


#8

You should be able to change the code to:

case 'W':
y_location+=5;
break;
case 'S':
y_location-=5;
break;
case 'A':
x_location-=5;
break;
case 'D':
x_location+=5;
break;

This works because char is an integer type, and can be promoted to int without loss (see https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules).