0

So I am currently learning how to program in c and till now I've been making great process. I decided to make myself a matrix in which a character ('0') can be freely moved around using wasd. Now there is the issue of keys repeating themselves making it impossible to move into two directions. Perhaps you know how if you hold a key on your keyboard it repeats itself quickly. This causes if I press for example w and a to fist go one up and then just to the left and not vertically. Is there any sort of argument that I can put in my code to prevent this (I'm using getchar())

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
Maximilian
  • 19
  • 1
  • 5
  • 2
    Not with plain C. You will need to use a library (maybe [ncurses](https://en.wikipedia.org/wiki/Ncurses)?) or some Operating System specific code. – pmg Oct 18 '20 at 08:41
  • What you ask is not possible using ISO C. Therefore, you will have to use platform-specific extensions (either directly or through a cross-platform library). For this reason, I suggest that you add a tag to your question, in which you specify your operating system (e.g. "Windows", "Linux" or "MacOS"). – Andreas Wenzel Oct 18 '20 at 09:21
  • Although the ncurses library allows you to move the cursor around freely, as far as I can tell, it is not designed to tell you whether the user is currently holding a certain key or not. The ncurses [`curs_getch`](https://invisible-island.net/ncurses/man/curs_getch.3x.html) function informs the program only of actual keypresses. However, when programming for the [X Window System](https://en.wikipedia.org/wiki/X_Window_System), you can use the function [`XQueryKeyMap`](https://tronche.com/gui/x/xlib/input/XQueryKeymap.html). This could be what you are looking for. – Andreas Wenzel Oct 18 '20 at 13:28
  • Related: [How can I get the keyboard state in Linux?](https://stackoverflow.com/q/3649874/12149471) – Andreas Wenzel Oct 18 '20 at 13:46
  • If you are writing an SDL application, you can use the function [`SDL_GetKeyboardState`](http://wiki.libsdl.org/SDL_GetKeyboardState). However, as far as I know, SDL is mainly designed for graphical applications, such as games. I'm not sure if it will work in text mode. – Andreas Wenzel Oct 19 '20 at 09:13

0 Answers0