如何在 glut 中指定键盘的方向键?

发布于 2024-10-03 16:07:38 字数 120 浏览 2 评论 0原文

我想使用上、下、左、右作为 opengl + glut 应用程序控件的一部分。如何引用“keyboard”函数(我传递给 glutKeyboardFunc 的函数)内的按键?

I wanna use up, down, left, right as part of the controls of an opengl + glut application. How do I refer to the keys inside my 'keyboard' function (the one that I pass to glutKeyboardFunc)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-10-10 16:07:39

您需要 glutSpecialFunc

例如 -

#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320, 319,-240, 239);
    glClear (GL_COLOR_BUFFER_BIT);

    glFlush();
}

void
catchKey(int key, int x, int y)
{
    if(key == GLUT_KEY_LEFT)    
        printf("Left key is pressed\n");
    else if(key == GLUT_KEY_RIGHT)
        printf("Right key is pressed\n");
    else if(key == GLUT_KEY_DOWN)
        printf("Down key is pressed\n");
    else if(key == GLUT_KEY_UP)
        printf("Up key is pressed\n");
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Special key");
    init();
    glutSpecialFunc(catchKey);
    glutMainLoop();
}

You need glutSpecialFunc.

For example -

#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320, 319,-240, 239);
    glClear (GL_COLOR_BUFFER_BIT);

    glFlush();
}

void
catchKey(int key, int x, int y)
{
    if(key == GLUT_KEY_LEFT)    
        printf("Left key is pressed\n");
    else if(key == GLUT_KEY_RIGHT)
        printf("Right key is pressed\n");
    else if(key == GLUT_KEY_DOWN)
        printf("Down key is pressed\n");
    else if(key == GLUT_KEY_UP)
        printf("Up key is pressed\n");
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Special key");
    init();
    glutSpecialFunc(catchKey);
    glutMainLoop();
}
一腔孤↑勇 2024-10-10 16:07:39

您的函数应该类似于这些

void handleSpecialKeypress(int key, int x, int y) {
 switch (key) {
 case GLUT_KEY_LEFT:
  isLeftKeyPressed = true;
  if (!isRightKeyPressed) {
        DO SOMETHING HERE;
  }
  break;
 case GLUT_KEY_RIGHT:
  isRightKeyPressed = true;
  if (!isLeftKeyPressed) {
        DO SOMETHING HERE;
  }
  break;
 }
}

void handleSpecialKeyReleased(int key, int x, int y) {
 switch (key) {
 case GLUT_KEY_LEFT:
  isLeftKeyPressed = false;
  break;
 case GLUT_KEY_RIGHT:
  isRightKeyPressed = false;
  break;
 }
}

,并且您还需要在 main() 方法中调用这些

 glutSpecialFunc(handleSpecialKeypress);
 glutSpecialUpFunc(handleSpecialKeyReleased);
  • 变量 isRightKeyPressedisLeftKeyPressed 是我定义的全局变量。

your functions should be something like these

void handleSpecialKeypress(int key, int x, int y) {
 switch (key) {
 case GLUT_KEY_LEFT:
  isLeftKeyPressed = true;
  if (!isRightKeyPressed) {
        DO SOMETHING HERE;
  }
  break;
 case GLUT_KEY_RIGHT:
  isRightKeyPressed = true;
  if (!isLeftKeyPressed) {
        DO SOMETHING HERE;
  }
  break;
 }
}

void handleSpecialKeyReleased(int key, int x, int y) {
 switch (key) {
 case GLUT_KEY_LEFT:
  isLeftKeyPressed = false;
  break;
 case GLUT_KEY_RIGHT:
  isRightKeyPressed = false;
  break;
 }
}

and you also need to invoke these in your main() method

 glutSpecialFunc(handleSpecialKeypress);
 glutSpecialUpFunc(handleSpecialKeyReleased);
  • The variables isRightKeyPressed and isLeftKeyPressed are global variables I defined.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文