Motorola 各機型按鍵碼不統一,甚至同系列正負號相反;模擬器與實機亦有出入。好消息是除螢幕尺寸與按鍵碼外,Moto 低階機種皆支援 MIDP 2.0,API 行為一致。
| 按鍵 | V500 / V600 | V180 / V3 | 備註 |
|---|---|---|---|
| Left Softkey | 21 | -21 | |
| Right Softkey | 22 | -22 | |
| Up | 1 | -1 | |
| Left | 2 | -2 | |
| Down | 6 | -6 | |
| Right | 5 | -5 | |
| Star (*) | 42 | -42 |
// 以絕對值判斷,兼容正負差異
protected void keyPressed(int keyCode) {
int absCode = Math.abs(keyCode);
switch (absCode) {
case 1: handleUp(); break;
case 2: handleLeft(); break;
case 5: handleRight(); break;
case 6: handleDown(); break;
case 21: handleLeftSoftkey(); break;
case 22: handleRightSoftkey(); break;
case 42: handleStar(); break;
}
}
官方 motocoder.com 提供各機型按鍵碼文檔,建議開發前下載對應機型規格書。