Issues (4868)

api/src/Etemplate/KeyManager.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * EGroupware API: contains classes and key constants for generating shortcuts
4
 *
5
 * @link http://www.egroupware.org
6
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
7
 * @package api
8
 * @subpackage etemplate
9
 * @author Andreas Stöckel
10
 * @copyright (c) 2011 Stylite
11
 * @version $Id$
12
 */
13
14
namespace EGroupware\Api\Etemplate;
15
16
class KeyManager
17
{
18
	const BACKSPACE = 8;
19
	const TAB = 9;
20
	const ENTER = 13;
21
	const ESCAPE = 27;
22
	const DELETE = 46;
23
24
	const SPACE = 32;
25
26
	const PAGE_UP = 33;
27
	const PAGE_DOWN = 34;
28
29
	const ARROW_LEFT = 37;
30
	const ARROW_UP = 38;
31
	const ARROW_RIGHT = 39;
32
	const ARROW_DOWN = 40;
33
34
	const _0 = 48;
35
	const _1 = 49;
36
	const _2 = 50;
37
	const _3 = 51;
38
	const _4 = 52;
39
	const _5 = 53;
40
	const _6 = 54;
41
	const _7 = 55;
42
	const _8 = 56;
43
	const _9 = 57;
44
45
	const A = 65;
46
	const B = 66;
47
	const C = 67;
48
	const D = 68;
49
	const E = 69;
50
	const F = 70;
51
	const G = 71;
52
	const H = 72;
53
	const I = 73;
54
	const J = 74;
55
	const K = 75;
56
	const L = 76;
57
	const M = 77;
58
	const N = 78;
59
	const O = 79;
60
	const P = 80;
61
	const Q = 81;
62
	const R = 82;
63
	const S = 83;
64
	const T = 84;
65
	const U = 85;
66
	const V = 86;
67
	const W = 87;
68
	const X = 88;
69
	const Y = 89;
70
	const Z = 90;
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_LNUMBER on line 70 at column 7
Loading history...
71
72
	const F1 = 112;
73
	const F2 = 113;
74
	const F3 = 114;
75
	const F4 = 115;
76
	const F5 = 116;
77
	const F6 = 117;
78
	const F7 = 118;
79
	const F8 = 119;
80
	const F9 = 120;
81
	const F10 = 121;
82
	const F11 = 122;
83
	const F12 = 123;
84
85
	/**
86
	 * Converts the given key codes into translated key names.
87
	 */
88
	public static function key_name($keyCode)
89
	{
90
		// Keys which can be directly translated into ASCII chars
91
		if (($keyCode >= self::_0 && $keyCode <= self::_9) ||
92
			($keyCode >= self::A && $keyCode <= self::Z))
93
		{
94
			return chr($keyCode);
95
		}
96
97
		// Function keys
98
		if ($keyCode >= self::F1 && $keyCode <= self::F12)
99
		{
100
			return "F".($keyCode - EGW_KEY_F1 + 1);
101
		}
102
103
		// Special keys
104
		switch ($keyCode) {
105
			case self::BACKSPACE:
106
				return lang("Back");
107
			case self::TAB:
108
				return lang("Tab");
109
			case self::DELETE:
110
				return lang("Del");
111
			case self::SPACE:
112
				return lang("Space");
113
			case self::PAGE_UP:
114
				return lang("Pg up");
115
			case self::PAGE_DOWN:
116
				return lang("Pg down");
117
		}
118
119
		return "";
120
	}
121
122
	/**
123
	 * Generates the caption of the given shortcut and returns it
124
	 */
125
	public static function shortcut_caption($keyCode, $shift = false, $ctrl = false, $alt = false)
126
	{
127
		$elems = array();
128
129
		if ($shift)
130
		{
131
			$elems[] = lang("Shift");
132
		}
133
134
		if ($ctrl)
135
		{
136
			$elems[] = lang("Ctrl");
137
		}
138
139
		if ($alt)
140
		{
141
			$elems[] = lang("Alt");
142
		}
143
144
		$elems[] = self::key_name($keyCode);
145
146
		return implode(" + ", $elems);
147
	}
148
149
	/**
150
	 * Generates a shortcut structure which can be JSON encoded and send to the
151
	 * egw action system. This function and class could later be used to provide
152
	 * user defined shortcuts.
153
	 */
154
	public static function shortcut($keyCode, $shift = false, $ctrl = false, $alt = false)
155
	{
156
		return array(
157
			"keyCode" => $keyCode,
158
			"shift" => (boolean)$shift,
159
			"ctrl" => (boolean)$ctrl,
160
			"alt" => (boolean)$alt,
161
			"caption" => self::shortcut_caption($keyCode, $shift, $ctrl, $alt)
162
		);
163
	}
164
}
165
166