Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect a key press in console

Tags:

java

I want to execute a certain function when a user presses a key. This will be run in the console, and the code is in Java. How do I do this? I have almost zero knowledge of key presses/keydowns, so I could really use an explanation as well.

like image 394
LightMikeE Avatar asked Dec 09 '14 14:12

LightMikeE


People also ask

How do I know which key is pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

What is console ReadKey () in C#?

ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen).

What is ReadKey in VB net?

ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

How do I stop the console from closing in C#?

You can off course put #if DEBUG and #endif around the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...' when running from the ...


1 Answers

You can't detect an event in the command line environment. You should provide a GUI, and then you can use the KeyListener class to detect a keyboard event.

Alternatively you can read commands from standard input and then execute a proper function.

like image 53
SalGnt Avatar answered Oct 14 '22 18:10

SalGnt