17
17
*/
18
18
package com .osfans .trime .ime .keyboard ;
19
19
20
+ import static android .view .KeyEvent .isModifierKey ;
21
+
20
22
import android .content .Context ;
21
23
import android .graphics .drawable .Drawable ;
22
24
import android .text .TextUtils ;
28
30
import com .osfans .trime .util .YamlUtils ;
29
31
import java .util .List ;
30
32
import java .util .Map ;
33
+ import timber .log .Timber ;
31
34
32
35
/** {@link Keyboard 鍵盤}中的各個按鍵,包含單擊、長按、滑動等多種{@link Event 事件} */
33
36
public class Key {
@@ -45,12 +48,12 @@ public class Key {
45
48
public static final int [] KEY_STATE_PRESSED = {android .R .attr .state_pressed };
46
49
public static final int [][] KEY_STATES =
47
50
new int [][] {
48
- KEY_STATE_PRESSED_ON ,
49
- KEY_STATE_PRESSED_OFF ,
50
- KEY_STATE_NORMAL_ON ,
51
- KEY_STATE_NORMAL_OFF ,
52
- KEY_STATE_PRESSED ,
53
- KEY_STATE_NORMAL
51
+ KEY_STATE_PRESSED_ON , // 0
52
+ KEY_STATE_PRESSED_OFF , // 1
53
+ KEY_STATE_NORMAL_ON , // 2
54
+ KEY_STATE_NORMAL_OFF , // 3
55
+ KEY_STATE_PRESSED , // 4
56
+ KEY_STATE_NORMAL // 5
54
57
};
55
58
public static List <String > androidKeys ;
56
59
public static Map <String , Map <String , ?>> presetKeys ;
@@ -144,7 +147,7 @@ public Key(Context context, Keyboard parent, Map<String, Object> mk) {
144
147
} else if (composing == null && has_menu == null && paging == null ) {
145
148
send_bindings = false ;
146
149
}
147
- if ( isShift ()) mKeyboard . setmShiftKey ( this );
150
+ mKeyboard . setModiferKey ( getCode (), this );
148
151
key_text_size = YamlUtils .INSTANCE .getPixel (mk , "key_text_size" , 0 );
149
152
symbol_text_size = YamlUtils .INSTANCE .getPixel (mk , "symbol_text_size" , 0 );
150
153
key_text_color = Config .getColor (context , mk , "key_text_color" );
@@ -423,6 +426,30 @@ public int squaredDistanceFrom(int x, int y) {
423
426
return xDist * xDist + yDist * yDist ;
424
427
}
425
428
429
+ // Trime把function键消费掉了,因此键盘只处理function键以外的修饰键
430
+ public boolean isTrimeModifierKey () {
431
+ return isTrimeModifierKey (getCode ());
432
+ }
433
+
434
+ public static boolean isTrimeModifierKey (int keycode ) {
435
+ if (keycode == KeyEvent .KEYCODE_FUNCTION ) return false ;
436
+ return isModifierKey (keycode );
437
+ }
438
+
439
+ public void printModifierKeyState (String invalidKey ) {
440
+ if (isTrimeModifierKey ())
441
+ Timber .d (
442
+ "\t <TrimeInput>\t keyState() key=%s, isShifted=%s, on=%s, invalidKey=%s" ,
443
+ getLabel (), mKeyboard .hasModifier (getModifierKeyOnMask ()), on , invalidKey );
444
+ }
445
+
446
+ public void printModifierKeyState () {
447
+ if (isTrimeModifierKey ())
448
+ Timber .d (
449
+ "\t <TrimeInput>\t keyState() key=%s, isShifted=%s, on=%s" ,
450
+ getLabel (), mKeyboard .hasModifier (getModifierKeyOnMask ()), on );
451
+ }
452
+
426
453
/**
427
454
* Returns the drawable state for the key, based on the current state and type of the key.
428
455
*
@@ -431,7 +458,10 @@ public int squaredDistanceFrom(int x, int y) {
431
458
*/
432
459
public int [] getCurrentDrawableState () {
433
460
int [] states = KEY_STATE_NORMAL ;
434
- boolean isShifted = isShift () && mKeyboard .isShifted (); // 臨時大寫
461
+ boolean isShifted = isTrimeModifierKey () && mKeyboard .hasModifier (getModifierKeyOnMask ());
462
+ // only for modiferKey debug
463
+ if (isTrimeModifierKey ()) mKeyboard .printModifierKeyState ("getCurrentDrawableState" );
464
+
435
465
if (isShifted || on ) {
436
466
if (pressed ) {
437
467
states = KEY_STATE_PRESSED_ON ;
@@ -454,22 +484,60 @@ public int[] getCurrentDrawableState() {
454
484
return states ;
455
485
}
456
486
487
+ public int getModifierKeyOnMask () {
488
+ return getModifierKeyOnMask (getCode ());
489
+ }
490
+
491
+ public int getModifierKeyOnMask (int keycode ) {
492
+ if (keycode == KeyEvent .KEYCODE_SHIFT_LEFT || keycode == KeyEvent .KEYCODE_SHIFT_RIGHT )
493
+ return KeyEvent .META_SHIFT_ON ;
494
+ if (keycode == KeyEvent .KEYCODE_CTRL_LEFT || keycode == KeyEvent .KEYCODE_CTRL_RIGHT )
495
+ return KeyEvent .META_CTRL_ON ;
496
+ if (keycode == KeyEvent .KEYCODE_META_LEFT || keycode == KeyEvent .KEYCODE_META_RIGHT )
497
+ return KeyEvent .META_META_ON ;
498
+ if (keycode == KeyEvent .KEYCODE_ALT_LEFT || keycode == KeyEvent .KEYCODE_ALT_RIGHT )
499
+ return KeyEvent .META_ALT_ON ;
500
+ if (keycode == KeyEvent .KEYCODE_SYM ) return KeyEvent .META_SYM_ON ;
501
+ return 0 ;
502
+ }
503
+
457
504
public boolean isShift () {
458
505
int c = getCode ();
459
506
return (c == KeyEvent .KEYCODE_SHIFT_LEFT || c == KeyEvent .KEYCODE_SHIFT_RIGHT );
460
507
}
461
508
509
+ public boolean isCtrl () {
510
+ int c = getCode ();
511
+ return (c == KeyEvent .KEYCODE_CTRL_LEFT || c == KeyEvent .KEYCODE_CTRL_RIGHT );
512
+ }
513
+
514
+ public boolean isMeta () {
515
+ int c = getCode ();
516
+ return (c == KeyEvent .KEYCODE_META_LEFT || c == KeyEvent .KEYCODE_META_RIGHT );
517
+ }
518
+
519
+ public boolean isAlt () {
520
+ int c = getCode ();
521
+ return (c == KeyEvent .KEYCODE_ALT_LEFT || c == KeyEvent .KEYCODE_ALT_RIGHT );
522
+ }
523
+
524
+ public boolean isSys () {
525
+ int c = getCode ();
526
+ return (c == KeyEvent .KEYCODE_SYM );
527
+ }
528
+
462
529
// shift键在点击时是否触发锁定
463
530
public boolean isShiftLock () {
464
- switch (getClick ().getShiftLock ()) {
465
- case "long" :
466
- return false ;
467
- case "click" :
468
- return true ;
469
- }
531
+ String s = getClick ().getShiftLock ();
532
+ if ("long" .equals (s )) return false ;
533
+ if ("click" .equals (s )) return true ;
470
534
return !Rime .isAsciiMode ();
471
535
}
472
536
537
+ /**
538
+ * @param type 同文按键模式(点击/长按/滑动)
539
+ * @return
540
+ */
473
541
public boolean sendBindings (int type ) {
474
542
Event e = null ;
475
543
if (type > 0 && type <= EVENT_NUM ) e = events [type ];
0 commit comments