Java API 分類導覽 - java.awt.event.KeyEvent getKeyCode()

KeyEvent 類別 (class) 的 getKeyCode() 方法 (method) 用來取得鍵盤按鍵的整數值。



修飾子與參數
public int getKeyCode()


getKeyCode() 的回傳值 (return value) 為 int 型態 (type) 的整數常數,非物件 (object) ,不需要參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;
 
public class KeyEventDemo extends KeyAdapter {
    Frame frame;
    TextArea text;
     
    public static void main(String[] args) {
        new KeyEventDemo();
    }
     
    public KeyEventDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setSize(600, 400);
        text = new TextArea();
        text.addKeyListener(this);
        frame.add(text);
        frame.setVisible(true);
    }
     
    public void keyPressed(KeyEvent e) {
        int key1 = e.getKeyCode();
        char key2 = e.getKeyChar();
        
        switch (key1) {
            case KeyEvent.VK_ESCAPE:
                System.out.println("esc");
                break;
            
            case KeyEvent.VK_TAB:
                System.out.println("tab");
                break;
            
            case KeyEvent.VK_CAPS_LOCK:
                System.out.println("caps");
                break;
            
            case KeyEvent.VK_SHIFT:
                System.out.println("shift");
                break;  
            
            case KeyEvent.VK_CONTROL:
                System.out.println("control or ctrl");
                break;
            
            case KeyEvent.VK_ALT:
                System.out.println("option or alt");
                break;
            
            case KeyEvent.VK_WINDOWS:
                System.out.println("window");
                break;
            
            case 157:
                System.out.println("command");
                break;
            
            case KeyEvent.VK_SPACE:
                System.out.println("space");
                break;
            
            case KeyEvent.VK_DELETE:
                System.out.println("delete");
                break;
            
            case KeyEvent.VK_BACK_SPACE:
                System.out.println("backspace");
                break;
            
            case KeyEvent.VK_ENTER:
                System.out.println("return or enter");
                break;
            
            case KeyEvent.VK_HOME:
                System.out.println("home");
                break;  
            
            case KeyEvent.VK_END:
                System.out.println("end");
                break;
            
            case KeyEvent.VK_PAGE_UP:
                System.out.println("page up");
                break;

            case KeyEvent.VK_PAGE_DOWN:
                System.out.println("page down");
                break;
            
            case KeyEvent.VK_UP:
                System.out.println("up");
                break;
            
            case KeyEvent.VK_LEFT:
                System.out.println("left");
                break;
            
            case KeyEvent.VK_DOWN:
                System.out.println("down");
                break;
            
            case KeyEvent.VK_RIGHT:
                System.out.println("right");
                break;
            
            case KeyEvent.VK_CLEAR:
                System.out.println("clear");
                break;          
            
            default:
                System.out.println(key2);
        }
    }
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:KeyEventDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例的 KeyEventDemo 直接繼承 KeyAdapter 類別
public class KeyEventDemo extends KeyAdapter {


由於 KeyAdapter 已經實作 KeyListener 介面,因此這裡我們直接改寫 keyPressed() 方法,當鍵盤被按下就會執行 keyPressed() 。首先呼叫 KeyEvent 的 getKeyCode() 取得鍵盤值, getKeyChar() 取得鍵盤字元,若是按下的鍵盤為控制字元,就印出該控制字元的英文名稱,如果不是控制字元就印出該字元的符號
public void keyPressed(KeyEvent e) {
    int key1 = e.getKeyCode();
    char key2 = e.getKeyChar();
        
    switch (key1) {
        case KeyEvent.VK_ESCAPE:
            System.out.println("esc");
            break;
            
        case KeyEvent.VK_TAB:
            System.out.println("tab");
            break;
           
        case KeyEvent.VK_CAPS_LOCK:
            System.out.println("caps");
            break;
            
        case KeyEvent.VK_SHIFT:
            System.out.println("shift");
            break;  
            
        case KeyEvent.VK_CONTROL:
            System.out.println("control or ctrl");
            break;
            
        case KeyEvent.VK_ALT:
            System.out.println("option or alt");
            break;
            
        case KeyEvent.VK_WINDOWS:
            System.out.println("window");
            break;
            
        case 157:
            System.out.println("command");
            break;
            
        case KeyEvent.VK_SPACE:
            System.out.println("space");
            break;
            
        case KeyEvent.VK_DELETE:
            System.out.println("delete");
            break;
            
        case KeyEvent.VK_BACK_SPACE:
            System.out.println("backspace");
            break;
            
        case KeyEvent.VK_ENTER:
            System.out.println("return or enter");
            break;
           
        case KeyEvent.VK_HOME:
            System.out.println("home");
            break;  
            
        case KeyEvent.VK_END:
            System.out.println("end");
            break;
            
        case KeyEvent.VK_PAGE_UP:
            System.out.println("page up");
            break;
 
        case KeyEvent.VK_PAGE_DOWN:
            System.out.println("page down");
            break;
            
        case KeyEvent.VK_UP:
            System.out.println("up");
            break;
            
        case KeyEvent.VK_LEFT:
            System.out.println("left");
            break;
            
        case KeyEvent.VK_DOWN:
            System.out.println("down");
            break;
            
        case KeyEvent.VK_RIGHT:
            System.out.println("right");
            break;
            
        case KeyEvent.VK_CLEAR:
            System.out.println("clear");
            break;          
            
        default:
            System.out.println(key2);
    }
}


編譯後執行,結果如下



中英文術語對照
類別class
方法method
屬性field
回傳值return value


您可以繼續參考
事件處理


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html

沒有留言: