Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C++ Button Press Detection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 07 March 2012 - 09:16 AM

Hello!
How can I detect if an user pressed a button instead of getting him to type it/enter it.
Example:

printf("Choose an option");
printf("1.Eat");
printf("2.Sleep");

//get button press
if (buttonpress ==1) Eat();
if (buttonpress ==2) Sleep();

Instead of: scanf("%d",&choise); if(choise==1)Eat();

You can modify the code as you wish. I just need to understand the part of the code where you get the button. IF POSSIBLE.

Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 3064

Like
0Likes
Like

Posted 07 March 2012 - 11:30 AM

In Windows, generally you'd use getch(), defined in <conio.h>; in Linux, you'd use a function like this (I can't remember which headers are required):

#include <stdio.h>
#include <sys/select.h>
#include <termios.h>
#include <stropts.h>
#include <unistd.h>

char getch(void){
    struct termios oldt,newt;
    char ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    scanf (" %c", &ch);
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
}

Other than that, getting a certain character without requiring the user to press Return is pretty platform specific, and you will need to research methods for your OS/window manager.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS