Advertisement
metalx1000

GCC Get Key Being Pressed

Dec 10th, 2022 (edited)
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /*
  2.  * This file is part of the FBK C code Examples distribution (https://filmsbyrkis.com).
  3.  * Copyright (c) 2022 Kris Occhipinti.
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, version 3.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. // This displays keys being pressed
  19. // WASD will show UP, LEFT, DOWN, RIGHT
  20.  
  21. //Dependencies:
  22. // sudo apt-get install libncurses5-dev libncursesw5-dev
  23.  
  24. //Compile:
  25. // gcc main.c -o main -W -Wall -Werror -Wextra -lncurses
  26.  
  27. #include <stdio.h>
  28. #include <curses.h>
  29.  
  30. void get_char();
  31.  
  32. int main(void)
  33. {
  34.   get_char();
  35.   return 0;
  36. }
  37.  
  38. void get_char()
  39. {
  40.   while (1){
  41.     initscr();  // mandatory curses init
  42.     cbreak();   // unbuffered input
  43.     int chr;
  44.  
  45.     do {
  46.       chr = getch();
  47.       clear();
  48.     } while (chr == ERR);
  49.  
  50.     if(chr == 97){
  51.       printw("LEFT\n");
  52.     }
  53.     else if(chr == 100){
  54.       printw("RIGHT\n");
  55.     }
  56.     else if(chr == 115){
  57.       printw("DOWN\n");
  58.     }
  59.     else if(chr == 119){
  60.       printw("UP\n");
  61.     }
  62.     else
  63.     {
  64.       printw("chr = %i\n", chr);
  65.     }
  66.     refresh();  // show the printf stuff
  67.  
  68.   }
  69.   endwin();   // curses cleanup
  70. }
  71.  
Tags: keys getchar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement