Instant Keyboard Input Questions

This is a discussion on Instant Keyboard Input Questions within the C Programming forums, part of the General Programming Boards category; I am starting to use the basic graphics in C, and I am trying to get keyboard input to work ...

  1. #1
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18

    Question Instant Keyboard Input Questions

    I am starting to use the basic graphics in C, and I am trying to get keyboard input to work right. I want something like getch() to recognize what key I pressed and save it in a variable so I can use that variable to control an object's movement on the screen. Here I have a basic box moving code.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <graphics.h>
    using namespace std;int main(void)
    {
        int gd=DETECT,gm;
        initgraph(&gd, &gm, "C:\\TC\\BGI");
        int ch;
        ch=getch();/*can i use this to bind getch() to a variable?*/
        int x=200, y=300, x2=400, y2=400;
        
        
        while(1==1){
        getch();
        cleardevice();
        if(ch==/*need help here*/){
        x+=5;
        y-=10;
        x2+=5;
        y2-=10;              
    }
        else if(ch==/*need help here*/){
        x-=5;
        y+=10;
        x2-=5;
        y2+=10;
    }
        rectangle(x,y,x2,y2);
    }
    }
    I put comments in the code to show you where I need help. I don
    t know if I can even bind a variable to getch(). I have searched the web for this and haven't found a good answer.

  2. #2
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18
    Can I have some help with this?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It seems like what you need to do is assign the return value of getch() to 'ch' inside the loop then evaluate it. For example:

    Code:
    while(1) {
        ch = getch();
        if(ch == 'a') {
           // update
        }
        else if(ch == 'd') {
          // update
        }
        // do other stuff like reprint screen?
    }

  4. #4
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18
    thanks a ton, it does make sense that you have to make the variable equal to getch() each time you want to change it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input from keyboard
    By B_B in forum C Programming
    Replies: 2
    Last Post: 10-25-2010, 01:27 PM
  2. Keyboard input
    By Darkinyuasha1 in forum Windows Programming
    Replies: 7
    Last Post: 06-19-2007, 04:21 PM
  3. Need help with keyboard input
    By wiramu in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2003, 02:44 PM
  4. Keyboard Input
    By stuartbut in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 11:09 AM
  5. Keyboard Input
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2002, 12:41 PM

Tags for this Thread


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