修飾子
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_ESCAPE esc 跳出鍵 VK_TAB tab 定位鍵 VK_CAPS_LOCK 英文字母大寫鎖定鍵 VK_SHIFT shift 鍵 VK_CONTROL control 或 ctrl 鍵 VK_ALT option 或 alt 鍵 VK_WINDOWS Windows 圖示鍵, Mac 系統 command 鍵的值為 157 VK_SPACE 空格鍵 VK_DELETE 刪除鍵 VK_BACK_SPACE 倒退刪除鍵 VK_ENTER 輸入鍵 VK_HOME home 鍵 VK_END end 鍵 VK_PAGE_UP page up 鍵 VK_PAGE_DOWN page 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
沒有留言:
張貼留言