program-o-steve
program-o-steve

Reputation: 2678

printing the ascii codes for small alphabets

How can i print the ascii codes of both the capital and small alphabets . ? For example on pressing q from keyboard,evt.getKeyCode() gives me 81 which is the ASCII code for capital Q. How can i print the ascii code for small alphabets ?

Upvotes: 0

Views: 1744

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109557

The KeyEvent.getKeyCode() only for the basic letters and digits returns the ASCII code. You get 65 for A with ASCII code 65 and a with ASCII code 65+32. With !evt.isShiftDown() you can then say it was an a.

The key codes are invented by Java, so called virtual keys. The constant VK_A is haphazardly chosen to be the ASCII code of A/a.

Upvotes: 1

jlink
jlink

Reputation: 692

As in ASCII A = 65 and a = 97, we see there is an offset of 97-65 = 32 between capital and lower case.

If you get Q = 81 you can add 32 to get 113 = q.

char c = (char)(evt.getKeyCode() + 32);

Upvotes: 1

Related Questions