aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/connectbot/service/KeyEventUtil.java
blob: 8ad645d86112198ff1d9ac544ba15b5462e2ab4e (plain)
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
/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2014 Torne Wuff
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.connectbot.service;

import android.view.KeyEvent;

public class KeyEventUtil {
	static final char CONTROL_LIMIT = ' ';
	static final char PRINTABLE_LIMIT = '\u007e';
	static final char[] HEX_DIGITS = new char[] {
		'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
	};

	static String printableRepresentation(String source) {
		if (source == null)
			return null;

		final StringBuilder sb = new StringBuilder();
		final int limit = source.length();
		char[] hexbuf = null;
		int pointer = 0;

		sb.append('"');
		while (pointer < limit) {
			int ch = source.charAt(pointer++);
			switch (ch) {
			case '\0':
				sb.append("\\0");
				break;
			case '\t':
				sb.append("\\t");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			default:
				if (CONTROL_LIMIT <= ch && ch <= PRINTABLE_LIMIT) {
					sb.append((char) ch);
				} else {
					sb.append("\\u");
					if (hexbuf == null)
						hexbuf = new char[4];
					for (int offs = 4; offs > 0; ) {
						hexbuf[--offs] = HEX_DIGITS[ch & 0xf];
						ch >>>= 4;
					}
					sb.append(hexbuf, 0, 4);
				}
			}
		}
		return sb.append('"').toString();
	}

	public static String describeKeyEvent(int keyCode, KeyEvent event) {
		StringBuilder d = new StringBuilder();
		d.append("keyCode=").append(keyCode);
		d.append(", keyCodeToString=").append(KeyEvent.keyCodeToString(keyCode));
		d.append(", event.toString=").append(event.toString());
		d.append(", action=").append(event.getAction());
		d.append(", characters=").append(printableRepresentation(event.getCharacters()));
		d.append(", deviceId=").append(event.getDeviceId());
		d.append(", displayLabel=").append((int) event.getDisplayLabel());
		d.append(", flags=0x").append(Integer.toHexString(event.getFlags()));
		d.append(", printingKey=").append(event.isPrintingKey());
		d.append(", keyCode=").append(event.getKeyCode());
		d.append(", metaState=0x").append(Integer.toHexString(event.getMetaState()));
		d.append(", modifiers=0x").append(Integer.toHexString(event.getModifiers()));
		d.append(", number=").append((int) event.getNumber());
		d.append(", scanCode=").append(event.getScanCode());
		d.append(", source=").append(event.getSource());
		d.append(", unicodeChar=").append(event.getUnicodeChar());
		return d.toString();
	}
}