-
Notifications
You must be signed in to change notification settings - Fork 1
/
section-06.html
280 lines (253 loc) · 7.81 KB
/
section-06.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="./main.css" rel="stylesheet">
</head>
<body>
<form>
<label for="input">Input</label>
<textarea id="input" rows="2"></textarea>
<div id="keyboards-list"><span>Keyboards:</span></div>
<div id="keyboard"></div>
<button id="submit" type="button">Submit</button>
<hr>
<label>Output</label>
<pre id="output"></pre>
</form>
</body>
<script>
/*
We're organising all the keyboard logic into a single 'KeyboardApp'.
*/
class KeyboardApp {
constructor () {
// Let's register all the HTML elements to make them easier to access later.
this.html = {
input: document.getElementById('input'),
keyboard: document.getElementById('keyboard'),
keyboardsList: document.getElementById('keyboards-list'),
submit: document.getElementById('submit'),
output: document.getElementById('output'),
}
// Let's register some event handling
this.html.input.onkeypress = this.onKeyPress.bind(this)
this.html.submit.onclick = this.submit.bind(this)
// Let's prepare the list of selectable keyboards
this.setupKeyboardsList()
// Let's set the default keyboard language
this.activeKeyboard = EMOJI_KEYBOARD
this.setupKeyboard()
}
/*
This 'submit' logic is just placeholder for what you actually want to do.
Normally, this is where we'd submit the input data to a server. For our
example, we just print the text into the output field.
*/
submit () {
const text = input.value
output.innerText = text
}
/*
setupKeyboardsList() does exactly what it says on the tin.
*/
setupKeyboardsList () {
Object.entries(KEYBOARDS).forEach(([language, keyboard]) => {
const buttonKeyboard = document.createElement('button')
buttonKeyboard.type = 'button'
buttonKeyboard.innerText = language
buttonKeyboard.onclick = () => {
this.activeKeyboard = keyboard
this.setupKeyboard()
}
this.html.keyboardsList.appendChild(buttonKeyboard)
})
}
/*
setupKeyboard() does exactly what it says on the tin.
*/
setupKeyboard () {
// First, we erase everything in the current keyboard.
while (this.html.keyboard.firstChild) {
this.html.keyboard.removeChild(this.html.keyboard.firstChild)
}
// If there's no active keyboard, I guess we've got nothing to do!
if (!this.activeKeyboard) return
// Right, now let's add the keys (buttons) for a QWERTY layout
QWERTY_LAYOUT.forEach((row) => {
const divRow = document.createElement('div')
divRow.className = 'row'
row.forEach((key) => {
// Create a button for each key in the QWERTY layout
const buttonKey = document.createElement('button')
buttonKey.className = 'keyboard-key'
buttonKey.type = 'button'
// Is there a character in our custom language keyboard that corresponds
// with this QWERTY key? If so, then pressing the key should insert
// the associated character.
const char = this.activeKeyboard[key]
if (char) {
buttonKey.innerText = char
buttonKey.onclick = () => { this.insertChar(char) }
} else {
buttonKey.innerText = ' '
}
// Add the 'shortcut' (i.e. the QWERTY key)
const shortcutSpan = document.createElement('span')
shortcutSpan.innerText = '(' + ENGLISH_KEYBOARD[key] + ')'
buttonKey.appendChild(shortcutSpan)
divRow.appendChild(buttonKey)
})
this.html.keyboard.appendChild(divRow)
})
}
/*
insertChar() inserts a single character to the (end of the) input text box.
*/
insertChar (char) {
// Find the position of the "text cursor" on the text input
const startIndex = this.html.input.selectionStart
const endIndex = this.html.input.selectionEnd
// Minor trivia: if the text cursor has selected some text (e.g. a user
// highlighted a word) then startIndex will not be the same as endIndex ;
// otherwise, startIndex will be the same as endIndex.
// Insert the new character where the text cursor is, OR replace the text
// that the text cursor is selecting.
const text = this.html.input.value
const startText = text.substring(0, startIndex)
const endText = text.substring(endIndex)
this.html.input.value = startText + char + endText
// Return focus to the input text box, and reset the "text cursor" to the
// location where we just inserted the character
this.html.input.focus()
const focusIndex = startIndex + char.length
this.html.input.setSelectionRange(focusIndex, focusIndex)
}
/*
onKeyPress listens for keyboard input on the text input field.
*/
onKeyPress (keyboardEvent) {
// Get the key that the user just pressed
const keyPressed = keyboardEvent.code
// Note: there are different ways to get what the user typed into a text
// field.
// keyboardEvent.code corresponds to the PHYSICAL key on the keyboard.
// keyboardEvent.key corresponds to the TEXT VALUE of the key.
// If a user presses the 'A' key on a US-International QWERTY keyboard,
// we get code='KeyA', and key='a' (if shift/capslock is off) or key='A'
// (if shift/capslock is on)
// Check to see if there's a character on the on-screen keyboard that
// corresponds to the key the user just pressed. If yes, insert that
// character.
const char = this.activeKeyboard && this.activeKeyboard[keyPressed]
if (char) {
this.insertChar(char)
return stopEvent(keyboardEvent)
}
return true
}
}
/*
Stops the default actions for an input event. For example, pressing the 'A' key
on a keyboard will insert the character 'a' into a text input field. If we want
to change the action so the 'A' key inserts the 'あ' character, we first need to
disable the default action.
*/
function stopEvent (e) {
e.preventDefault && e.preventDefault()
e.stopPropagation && e.stopPropagation()
return false
}
/*
English Keyboard + QWERTY Layout: defines the baseline US-International QWERTY
keyboard that we assume each user is using.
*/
const ENGLISH_KEYBOARD = {
'KeyQ': 'Q',
'KeyW': 'W',
'KeyE': 'E',
'KeyR': 'R',
'KeyT': 'T',
'KeyY': 'Y',
'KeyU': 'U',
'KeyI': 'I',
'KeyO': 'O',
'KeyP': 'P',
// --------
'KeyA': 'A',
'KeyS': 'S',
'KeyD': 'D',
'KeyF': 'F',
'KeyG': 'G',
'KeyH': 'H',
'KeyJ': 'J',
'KeyK': 'K',
'KeyL': 'L',
// --------
'KeyZ': 'Z',
'KeyX': 'X',
'KeyC': 'C',
'KeyV': 'V',
'KeyB': 'B',
'KeyN': 'N',
'KeyM': 'M',
}
const QWERTY_LAYOUT = [
[ 'KeyQ', 'KeyW', 'KeyE', 'KeyR', 'KeyT', 'KeyY', 'KeyU', 'KeyI', 'KeyO', 'KeyP' ],
[ 'KeyA', 'KeyS', 'KeyD', 'KeyF', 'KeyG', 'KeyH', 'KeyJ', 'KeyK', 'KeyL' ],
[ 'KeyZ', 'KeyX', 'KeyC', 'KeyV', 'KeyB', 'KeyN', 'KeyM' ]
]
/*
Japanese Keyboard: translates key presses on a US-International QWERTY keyboard
into (a small subset of) Japanese hiragana characters.
*/
const JAPANESE_KEYBOARD = {
'KeyA': 'あ',
'KeyI': 'い',
'KeyU': 'う',
'KeyE': 'え',
'KeyO': 'お',
}
/*
Emoji Keyboard: a joke keyboard to demonstrate how easy it is to add map keys
to characters of any 'language'.
*/
const EMOJI_KEYBOARD = {
'KeyQ': '😃',
'KeyW': '😅',
'KeyE': '🤣',
'KeyR': '🥰',
'KeyT': '😉',
'KeyY': '😂',
'KeyU': '🥵',
'KeyI': '🥶',
'KeyO': '😍',
'KeyP': '😵',
// --------
'KeyA': '🐈',
'KeyS': '🐕',
'KeyD': '🐒',
'KeyF': '🕊️',
'KeyG': '🐓',
'KeyH': '🐟',
'KeyJ': '🐙',
'KeyK': '🦑',
'KeyL': '🦋',
// --------
'KeyZ': '❤️',
'KeyX': '🧡',
'KeyC': '💛',
'KeyV': '💚',
'KeyB': '💙',
'KeyN': '💜',
'KeyM': '🤍',
}
const KEYBOARDS = {
'(No keyboard)': undefined,
'English': ENGLISH_KEYBOARD,
'Japanese': JAPANESE_KEYBOARD,
'Emoji': EMOJI_KEYBOARD,
}
var app = new KeyboardApp() // Let's start the app!
</script>
</html>