Getting Keyboard Input

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



Leave a reply



Submit