A Java/Scala program to print key code values using java.awt.event.KeyEvent

The following Scala source code shows the value of various keys on the keyboard that I was interested in today:

package com.valleyprogramming.typewriter
 
import java.awt.event.KeyEvent
 
/**
 * This is a series of tests to see what integer values are returned
 * by the following `KeyEvent._` values. This helps me write a match/case
 * expression to handle the keys properly.
 */
object KeyTests extends App {
 
    println("shift:     " + KeyEvent.VK_SHIFT)
    println("control:   " + KeyEvent.VK_CONTROL)
    println("alt:       " + KeyEvent.VK_ALT)
    println("meta:      " + KeyEvent.VK_META)
    println("enter:     " + KeyEvent.VK_ENTER)
    println("backspace: " + KeyEvent.VK_BACK_SPACE)
    println("escape:    " + KeyEvent.VK_ESCAPE)
    println("left:      " + KeyEvent.VK_LEFT)
    println("right:     " + KeyEvent.VK_RIGHT)
    println("up:        " + KeyEvent.VK_UP)
    println("down:      " + KeyEvent.VK_DOWN)
    println("pg_down:   " + KeyEvent.VK_PAGE_DOWN)
    println("pg_up:     " + KeyEvent.VK_PAGE_UP)
 
    println("a:         " + KeyEvent.VK_A)
    println("z:         " + KeyEvent.VK_Z)
    
    println("space:     " + KeyEvent.VK_SPACE)
    println("tab:       " + KeyEvent.VK_TAB)
 
    // KEYS I WANT TO HANDLE
    // ---------------------
    // 32 = space
    // 33-96, 123-126 are standard keyboard characters
    // 97-122 are lowercase a-z (not sure i need them though, as VK_A is 65)
    // tab is 9    (VK_TAB)
    // ENTER
    // -----
    // LF is 10    (VK_ENTER)
    // FF is 12
    // CR is 13
    // OTHER
    // -----
    // BS is 8     (VK_BACK_SPACE)
    // SPACE is 32 (VK_SPACE)
 
/*
    shift:     16
    control:   17
    alt:       18
    meta:      157
    enter:     10
    backspace: 8
    escape:    27
    left:      37
    up:        38
    right:     39
    down:      40
    pg_down:   34
    pg_up:     33
    a:         65
    z:         90
    space:     32
    tab:       9
*/   
}

I just wanted a little program to print out these keystrokes (key codes), and this program did the trick. (I also wanted to know the key code for the Mac OS X Fn key; that turns out to be 63, which I’ve described elsewhere on this site.)