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

KeyEvent 類別 (class) 主要處理鍵盤按鍵相關事件,此為實作 KeyListener 介面 (interface) 後, keyTyped() 、 keyPressed() 、 keyReleased() 的參數 (parameter) e 即為 KeyEvent 物件 (object) 。



修飾子
public class KeyEvent


父類別
java.awt.event.InputEvent


實作介面
Serializable


建構子
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)


控制字元屬性
名稱敘述
VK_ESCAPEesc 跳出鍵
VK_TABtab 定位鍵
VK_CAPS_LOCK英文字母大寫鎖定鍵
VK_SHIFTshift 鍵
VK_CONTROLcontrol 或 ctrl 鍵
VK_ALToption 或 alt 鍵
VK_WINDOWSWindows 圖示鍵, Mac 系統 command 鍵的值為 157
VK_SPACE空格鍵
VK_DELETE刪除鍵
VK_BACK_SPACE倒退刪除鍵
VK_ENTER輸入鍵
VK_HOMEhome 鍵
VK_ENDend 鍵
VK_PAGE_UPpage up 鍵
VK_PAGE_DOWNpage down 鍵
VK_UP上方向鍵
VK_LEFT左方向鍵
VK_DOWN下方向鍵
VK_RIGHT右方向鍵
VK_CLEAR清除鍵


常用方法
名稱敘述
getKeyCode()取得按鍵的值
getKeyChar()取得按鍵的字元


舉例如下
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
介面interface
參數parameter
物件object


您可以繼續參考
事件處理


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


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

沒有留言: