getch

Non blocking getch()

喜你入骨 提交于 2019-11-29 10:54:21
I'm trying to make Tetris game in standard console. I need non-blocking getch(), so the blocks can fall without pressing any key. It would be nice to have function that returns -1 if no key pressed, otherwise the key code. It's operating system specific but your library probably has a function called kbhit() or similar that will do this This is exactly what you wanted: int getch_noblock() { if (_kbhit()) return _getch(); else return -1; } Basically kbhit() does the job of determining if a key is pressed. Assumes Windows and Microsoft Visual C++. 来源: https://stackoverflow.com/questions/11472043

Python Windows `msvcrt.getch()` only detects every 3rd keypress?

允我心安 提交于 2019-11-28 10:30:53
My code is below: import msvcrt while True: if msvcrt.getch() == 'q': print "Q was pressed" elif msvcrt.getch() == 'x': sys.exit() else: print "Key Pressed:" + str(msvcrt.getch() This code is based on this question ; I was using it to acquaint myself with getch . I've noticed that it takes 3 pressing the key 3 times to output the text once. Why is this? I'm trying to use it as an event loop, and that's too much of a lag... Even if I type 3 different keys, it only outputs the 3rd keypress. How can I force it to go faster? Is there a better way to achieve what I'm trying to achieve? Thanks!

C exit from infinite loop on keypress

感情迁移 提交于 2019-11-28 10:07:01
How can I exit from an infinite loop, when a key is pressed? Currently I'm using getch, but it will start blocking my loop as soon, as there is no more input to read. Talha Ahmed Khan I would suggest that you go throgh this article. Non-blocking user input in loop without ncurses. Rudy Velthuis If you are using getch() from conio.h anyway, try to use kbhit() instead. Note that both getch() and kbhit() - conio.h , in fact - are not standard C. The function kbhit() from conio.h returns non-zero value if any key is pressed but it does not block like getch() . Now, this is obviously not standard.

Python/curses user input while updating screen

旧城冷巷雨未停 提交于 2019-11-28 04:54:35
问题 I'm currently coding an app U.I with python/curses and I was wondering if it is possible to ask the user to press keys (cbreak mode) to hide or show some panels or windows while the U.I is continuously updating. I read the official python docs about curses and made some tries but even with the use of the cbreak mode and the non-blocking input mode (nodelay) activated I am unable to make it work properly (I succeed in getting the user input but at the expense of blocking the U.I that is not

getch and putchar not working without return

↘锁芯ラ 提交于 2019-11-28 01:44:50
I have been trying to get getch to work in another program with no success. So I have made the most basic program I can using getch the way I want it to work in the main program. I have researched the need for noecho , cbreak , initscr and nodelay , I have also tried using newscr() but to no success. The problem I am having is that the chars aren't being printed to the screen till I hit "enter", when they should be put to the screen every loop. Why is this happening? Also the cursor doesn't return to the left of the screen at the new line. eg. abc def ghi I have looked for the answer but am

How to run program whilst listening for user input in C?

纵饮孤独 提交于 2019-11-28 00:32:35
I'm trying to make a game that continues running until a key is pressed and then it should take that key in and do something with it then continue running as per normal. How do I do this? I'm on MAC so even though I've come across a windows library called conio.h which can handle this using kbhit() and getch(), I can't get it working for me... // // main.c // conioTesting // // #include <stdio.h> #include "myconio_mac.h" int main(int argc, const char * argv[]) { int counter = 0; while (counter < 2) { if (kbhit()) { char key = getch(); printf("\n Key is %c \n", key); printf("Keyboard hit

Non-blocking getch(), ncurses

浪尽此生 提交于 2019-11-27 19:46:11
I'm having some problems getting ncurses' getch() to block. Default operation seems to be non-blocking (or have I missed some initialization)? I would like it to work like getch() in Windows. I have tried various versions of timeout(3000000); nocbreak(); cbreak(); noraw(); etc... (not all at the same time). I would prefer to not (explicitly) use any WINDOW , if possible. A while loop around getch(), checking for a specific return value is OK too. The curses library is a package deal. You can't just pull out one routine and hope for the best without properly initializing the library. Here's a

Equivalent to Windows getch() for Mac/Linux crashes

我是研究僧i 提交于 2019-11-27 14:46:45
I am using getch() and my app crashes instantly. Including when doing: int main() { getch(); } I can't find the link but supposedly the problem is that it needs to turn off buffering or something strange along those lines, and I still want cout to work along with cross platform code. I was told to use std::cin.get() , but I'd like the app to quit when a key is pressed, not when the user typed in a letter or number then press enter to quit. Is there any function for this? The code must work under Mac (my os) and Windows. Linking/compiling is not an issue ; I include <curses.h> and link with

How to use kbhit and getch (C programming) [closed]

痴心易碎 提交于 2019-11-27 09:43:47
I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P , if the user presses P then it will break the loop. However I don't think I'm using _kbhit and _getch properly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion void activateAlarm(int channelID) { int key = 0; while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit ||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) { beep(350,100); if (_kbhit

Why getch() returns before press any key?

我与影子孤独终老i 提交于 2019-11-27 04:52:49
int main(int argc, char *argv[], char *env[]) { printf("Press any key to exit.\n"); getch(); return 0; } According to the man page, getch should wait until any key is pressed ...but in fact it returns directly before press any key. (The value returned is -1 ). Why? Update I'm on Linux. How can I implement Press any key to exit. , if not using getch() ? getchar() will only return after press Enter, it's not what I want. On Linux, getch() is probably the curses function, which is quite different from the Windows-specific function of the same name. curses ( ncurses ) is probably overkill for what