|
7 | 7 | public class ScanGunManager { |
8 | 8 |
|
9 | 9 | private final static long MESSAGE_DELAY = 500; // 延迟500ms,判断扫码是否完成。 |
10 | | - private StringBuffer mStringBufferResult; // 扫码内容 |
11 | | - private boolean mCaps; // 大小写区分 |
| 10 | + private StringBuffer stringBufferResult; // 扫码内容 |
12 | 11 |
|
13 | 12 | private ScanCodeCallback scanCodeCallback; |
14 | 13 |
|
15 | 14 | public interface OnScanCodeListener { |
16 | 15 | void onScanCode(String value); |
17 | 16 | } |
18 | 17 |
|
19 | | - |
20 | 18 | // 类初始化时,不初始化这个对象(延时加载,真正用的时候再创建) |
21 | 19 | private static ScanGunManager instance; |
22 | 20 |
|
23 | 21 | // 构造器私有化 |
24 | 22 | private ScanGunManager(){} |
25 | 23 |
|
26 | | - // 方法同步,调用效率低 |
| 24 | + // 限制线程同时访问 jvm 中该类的所有实例同时访问对应的代码块 |
27 | 25 | public static synchronized ScanGunManager getInstance(){ |
28 | | - if(instance==null){ |
| 26 | + if (instance == null) { |
29 | 27 | instance=new ScanGunManager(); |
30 | | - instance.mStringBufferResult = new StringBuffer(); |
| 28 | + instance.stringBufferResult = new StringBuffer(); |
31 | 29 | } |
32 | 30 | return instance; |
33 | 31 | } |
34 | 32 |
|
35 | | - |
36 | 33 | public void setReceiveCallback(ScanCodeCallback scanCodeCallback) { |
37 | 34 | this.scanCodeCallback = scanCodeCallback; |
38 | 35 | } |
39 | 36 |
|
40 | 37 | public void clearScanBarCodeText(){ |
41 | | - if (mStringBufferResult!= null){ |
42 | | - mStringBufferResult.delete(0,mStringBufferResult.length()); |
| 38 | + if (stringBufferResult != null){ |
| 39 | + stringBufferResult.delete(0, stringBufferResult.length()); |
43 | 40 | } |
44 | 41 | } |
45 | 42 |
|
46 | 43 | // 外设事件解析 |
47 | 44 | public void analysisKeyEvent(KeyEvent event) { |
48 | 45 | int keyCode = event.getKeyCode(); |
49 | | - String devicesName = event.getDevice().getName(); |
50 | | - Log.d(TAG, "设备名称:" + devicesName); |
51 | | - // 字母大小写判断 |
52 | | - checkLetterStatus(event); |
53 | | - if (event.getAction() == KeyEvent.ACTION_DOWN) { |
54 | | - char aChar = getInputCode(event);; |
55 | | - if (aChar != 0) { |
56 | | - if (mStringBufferResult!=null){ |
57 | | - mStringBufferResult.append(aChar); |
58 | | - } |
59 | | - } |
| 46 | + boolean isShiftPressed = event.isShiftPressed(); |
| 47 | + // 对按下的键进行操作,过滤 shift |
| 48 | + if ( |
| 49 | + event.getAction() == KeyEvent.ACTION_DOWN |
| 50 | + && keyCode != KeyEvent.META_SHIFT_ON |
| 51 | + && keyCode != KeyEvent.KEYCODE_SHIFT_LEFT |
| 52 | + && keyCode != KeyEvent.KEYCODE_SHIFT_RIGHT |
| 53 | + ) { |
60 | 54 | if (keyCode == KeyEvent.KEYCODE_ENTER) { |
61 | | - String barCode = mStringBufferResult.toString(); |
| 55 | + // 输入结束,返回字符串 |
| 56 | + String barCode = stringBufferResult.toString(); |
62 | 57 | if (scanCodeCallback != null) { |
63 | 58 | scanCodeCallback.onScanCode(barCode); |
64 | 59 | } |
65 | | - mStringBufferResult.delete(0,mStringBufferResult.length()); |
| 60 | + stringBufferResult.delete(0, stringBufferResult.length()); |
| 61 | + } else { |
| 62 | + // 处理输入的字符串 |
| 63 | + Log.d(TAG, "event:" + event + "\nkeyCode:" + event.getKeyCode() + "\nisShiftPressed:" + event.isShiftPressed()); |
| 64 | + char inputCode = getInputCode(keyCode, isShiftPressed);; |
| 65 | + if (inputCode != 0) { |
| 66 | + if (stringBufferResult != null){ |
| 67 | + stringBufferResult.append(inputCode); |
| 68 | + } |
| 69 | + } |
66 | 70 | } |
67 | 71 | } |
68 | 72 | } |
69 | 73 |
|
70 | | - //检查shift键 |
71 | | - private void checkLetterStatus(KeyEvent event) { |
72 | | - int keyCode = event.getKeyCode(); |
73 | | - if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) { |
74 | | - mCaps = event.getAction() == KeyEvent.ACTION_DOWN; |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - |
79 | 74 | //获取扫描内容 |
80 | | - private char getInputCode(KeyEvent event) { |
81 | | - int keyCode = event.getKeyCode(); |
82 | | - char aChar; |
| 75 | + private char getInputCode(int keyCode, boolean isShiftPressed) { |
| 76 | + char inputCode; |
83 | 77 |
|
84 | 78 | if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) { |
85 | | - //字母 |
86 | | - aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A); |
87 | | - } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { |
88 | | - //数字 |
89 | | - aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0); |
| 79 | + // 大小写字母 |
| 80 | + inputCode = (char) ((isShiftPressed ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A); |
90 | 81 | } else { |
91 | | - //其他符号 |
92 | | - aChar = keyValue(mCaps, keyCode); |
| 82 | + // 数字及其他符号(限英文字符) |
| 83 | + inputCode = keyValue(keyCode, isShiftPressed); |
93 | 84 | } |
94 | | - return aChar; |
| 85 | + Log.d(TAG, "result keyCode:" + inputCode + "keyCode=" +keyCode); |
| 86 | + |
| 87 | + return inputCode; |
95 | 88 | } |
96 | 89 |
|
97 | 90 | /** |
98 | 91 | * 按键对应的char表 |
99 | 92 | */ |
100 | | - private char keyValue(boolean caps, int keyCode) { |
| 93 | + private char keyValue(int keyCode, boolean isShiftPressed) { |
101 | 94 | switch (keyCode) { |
102 | 95 | case KeyEvent.KEYCODE_0: |
103 | | - return caps ? ')' : '0'; |
| 96 | + return isShiftPressed ? ')' : '0'; |
104 | 97 | case KeyEvent.KEYCODE_1: |
105 | | - return caps ? '!' : '1'; |
| 98 | + return isShiftPressed ? '!' : '1'; |
106 | 99 | case KeyEvent.KEYCODE_2: |
107 | | - return caps ? '@' : '2'; |
| 100 | + return isShiftPressed ? '@' : '2'; |
108 | 101 | case KeyEvent.KEYCODE_3: |
109 | | - return caps ? '#' : '3'; |
| 102 | + return isShiftPressed ? '#' : '3'; |
110 | 103 | case KeyEvent.KEYCODE_4: |
111 | | - return caps ? '$' : '4'; |
| 104 | + return isShiftPressed ? '$' : '4'; |
112 | 105 | case KeyEvent.KEYCODE_5: |
113 | | - return caps ? '%' : '5'; |
| 106 | + return isShiftPressed ? '%' : '5'; |
114 | 107 | case KeyEvent.KEYCODE_6: |
115 | | - return caps ? '^' : '6'; |
| 108 | + return isShiftPressed ? '^' : '6'; |
116 | 109 | case KeyEvent.KEYCODE_7: |
117 | | - return caps ? '&' : '7'; |
| 110 | + return isShiftPressed ? '&' : '7'; |
118 | 111 | case KeyEvent.KEYCODE_8: |
119 | | - return caps ? '*' : '8'; |
| 112 | + return isShiftPressed ? '*' : '8'; |
120 | 113 | case KeyEvent.KEYCODE_9: |
121 | | - return caps ? '(' : '9'; |
| 114 | + return isShiftPressed ? '(' : '9'; |
122 | 115 | case KeyEvent.KEYCODE_NUMPAD_SUBTRACT: |
123 | | - return '-'; |
| 116 | + return isShiftPressed ? '_' : '-'; |
124 | 117 | case KeyEvent.KEYCODE_MINUS: |
125 | | - return '_'; |
| 118 | + return isShiftPressed ? '_' : '-'; |
126 | 119 | case KeyEvent.KEYCODE_EQUALS: |
127 | | - return '='; |
| 120 | + return isShiftPressed ? '+' : '='; |
128 | 121 | case KeyEvent.KEYCODE_NUMPAD_ADD: |
129 | | - return '+'; |
| 122 | + return isShiftPressed ? '+' : '='; |
130 | 123 | case KeyEvent.KEYCODE_GRAVE: |
131 | | - return caps ? '~' : '`'; |
| 124 | + return isShiftPressed ? '~' : '`'; |
132 | 125 | case KeyEvent.KEYCODE_BACKSLASH: |
133 | | - return caps ? '|' : '\\'; |
| 126 | + return isShiftPressed ? '|' : '\\'; |
134 | 127 | case KeyEvent.KEYCODE_LEFT_BRACKET: |
135 | | - return caps ? '{' : '['; |
| 128 | + return isShiftPressed ? '{' : '['; |
136 | 129 | case KeyEvent.KEYCODE_RIGHT_BRACKET: |
137 | | - return caps ? '}' : ']'; |
| 130 | + return isShiftPressed ? '}' : ']'; |
138 | 131 | case KeyEvent.KEYCODE_SEMICOLON: |
139 | | - return caps ? ':' : ';'; |
| 132 | + return isShiftPressed ? ':' : ';'; |
140 | 133 | case KeyEvent.KEYCODE_APOSTROPHE: |
141 | | - return caps ? '"' : '\''; |
| 134 | + return isShiftPressed ? '"' : '\''; |
142 | 135 | case KeyEvent.KEYCODE_COMMA: |
143 | | - return caps ? '<' : ','; |
| 136 | + return isShiftPressed ? '<' : ','; |
144 | 137 | case KeyEvent.KEYCODE_PERIOD: |
145 | | - return caps ? '>' : '.'; |
| 138 | + return isShiftPressed ? '>' : '.'; |
146 | 139 | case KeyEvent.KEYCODE_SLASH: |
147 | | - return caps ? '?' : '/'; |
| 140 | + return isShiftPressed ? '?' : '/'; |
| 141 | + case KeyEvent.KEYCODE_SPACE: |
| 142 | + return ' '; |
148 | 143 | default: |
| 144 | + Log.d(TAG, "键码为" + keyCode + "的按键未做处理或无法识别"); |
149 | 145 | return 0; |
150 | 146 | } |
151 | 147 | } |
|
0 commit comments