Keyboard | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Keyboard

How can I set the value of a variable via keyboard while the program is running ? :/

7th Feb 2017, 2:04 PM
Agamemnon Kyriazis
Agamemnon Kyriazis - avatar
5 Answers
+ 3
For better interactivity you can have a look at: keyPressed() , keyTyped(), and keyReleased() event listeners: https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html tutorial is here: https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html Simplier option: Processing IDE: you only have to write a keyPressed/released/typed function as the processing library handles the nitty gritty for you. and all info with contestual examples are available in the reference. https://processing.org
7th Feb 2017, 4:22 PM
seamiki
seamiki - avatar
+ 2
after you add the keylistener to an element of your program(Jframe, JtextField, image ecc...) element.addKeyListener(this); you can define your own function wherever inside the main. public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: ");//replace with your code }
10th Feb 2017, 4:14 PM
seamiki
seamiki - avatar
+ 1
Use Scanner x=new Scanner(System.in) or InputstreamReader...?
8th Feb 2017, 5:53 AM
Shreyance Gupta
Shreyance Gupta - avatar
0
can you please give me an example of the command and where should be written?
7th Feb 2017, 3:58 PM
Agamemnon Kyriazis
Agamemnon Kyriazis - avatar
0
Thanks a lot I will give it a try and I will inform you if I face any problem with it
7th Feb 2017, 4:39 PM
Agamemnon Kyriazis
Agamemnon Kyriazis - avatar