How to get direct keyboard input in java
UP DOWN LEFT and RIGHT as every character in the keyboard have a ASCII code, so you can actually check it out. Normally in Java, instead of checking if a key has been pressed you could use KeyEvents:
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_RIGHT: System.out.println("The right arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_LEFT: System.out.println("The left arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_UP: System.out.println("The up arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_DOWN: System.out.println("The down arrow key is
pressed");
// Other operations
break;
}
}
You have more information in the documentation
User key keyboard input
pygame
provides a function for getting the name of the key pressed:
pygame.key.name
And so you can use it to get the name of the key, there is no need to use a dictionary for this:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 400))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
key_name = pygame.key.name(event.key)
print(key_name)
Keyboard inputs in c without using a library that does everything for you
You will probably want to process the following two window messages in your message loop:
WM_KEYDOWN
WM_KEYUP
See the Microsoft documentation on Keyboard Input for further information.
Related Topics
How to Disable Jsessionid in Tomcat Servlet
Connecting an Input Stream to an Outputstream
How to Read the Manifest File for a Webapp Running in Apache Tomcat
Validate Jaxbelement in JPA/Jax-Rs Web Service
Best Way to Constrain User to Enter a Time in a Jtextfield
How to Determine If a Number Is a Prime with Regex
Differencebetween A.Getclass() and A.Class in Java
How to Write a Basic Swap Function in Java
When Should We Create Our Own Java Exception Classes
Current Time in Microseconds in Java
Setting Jvm Heap Size at Runtime
Using a Jfilechooser with Swing Gui Classes and Listeners
Static VS. Dynamic Binding in Java
How to Put All Required Jar Files in a Library Folder Inside the Final Jar File with Maven