GLUT Keyboard Reaction Help

This is a discussion on GLUT Keyboard Reaction Help within the Game Programming forums, part of the General Programming Boards category; Hey there! I finally got GLUT working, and am currently on my way to a complete program. But right now ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    47

    GLUT Keyboard Reaction Help

    Hey there!

    I finally got GLUT working, and am currently on my way to a complete program. But right now I just have a square that you can move around the screen.

    But I noticed that with the glutKeyboardFunc(), in order for you to move, you have to wait for the keyboard repeat delay. Is there anyway to fix this?

    Thanks!
    ~Matt

  2. #2
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Depending on what glut implementation you're using, there are keyboard up and down functions that should be in the docs .
    Illusion and reality become impartiality and confidence.

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Try something like this:

    Code:
    bool key_state[256] = { false };
    
    void update_func()
    {
        if(key_state['a'] == true) {
            // move left
        }
    
        if(key_state['d'] == true) {
            // move right
        }
    
        if(key_state['w'] == true) {
            // move up
        }
    
        if(key_state['s'] == true) {
            // move down
        }
    }
    
    void key_down_func(unsigned char key, int x, int y)
    {
        key_state[key] = true;
    }
    
    void key_up_func(unsigned char key, int x, int y)
    {
        key_state[key] = false;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
    
        glutInitWindowSize(800, 600);
        glutInitWindowPosition(0, 0);
    
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
        glutCreateWindow("Foobar");
    
        glutDisplayFunc(update_func);
        glutIdleFunc(update_func);
    
        glutKeyboardFunc(key_down_func);
        glutKeyboardUpFunc(key_up_func);
    
        glutIgnoreKeyRepeat(true);
    
        glutMainLoop();
    
        return 0;
    }

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Try something like this:
    Yeah, that's what I do. It works so well, I have to ADD a delay sometimes because the repeat rate is way too fast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 03:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. GLUT keyboard and mouse func separated
    By krappa in forum Game Programming
    Replies: 1
    Last Post: 04-20-2005, 07:27 PM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21