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

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



修飾子與參數
public int getKeyCode()


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


舉例如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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);
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:KeyEventDemo.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2012 年 3 月 */


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


由於 KeyAdapter 已經實作 KeyListener 介面,因此這裡我們直接改寫 keyPressed() 方法,當鍵盤被按下就會執行 keyPressed() 。首先呼叫 KeyEvent 的 getKeyCode() 取得鍵盤值, getKeyChar() 取得鍵盤字元,若是按下的鍵盤為控制字元,就印出該控制字元的英文名稱,如果不是控制字元就印出該字元的符號
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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

沒有留言: