-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyboard.coffee
124 lines (111 loc) · 3.37 KB
/
Keyboard.coffee
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
class Keyboard
constructor: (@xpra) ->
@caps_lock = false
@topwindow = 2
document.onkeydown = (args...) =>
@onkeydown args...
document.onkeyup = (args...) =>
@onkeyup args...
document.onkeypress = (args...) =>
@onkeypress args...
###*
# Returns the modifiers set for the current event.
# We get the list of modifiers using "get_event_modifiers"
# then translate "alt" and "meta" into their keymap name.
# (usually "mod1")
###
get_modifiers: (event) ->
#convert generic modifiers "meta" and "alt" into their x11 name:
modifiers = get_event_modifiers(event)
#FIXME: look them up!
alt = 'mod1'
meta = 'mod1'
index = modifiers.indexOf('alt')
if index >= 0
modifiers[index] = alt
index = modifiers.indexOf('meta')
if index >= 0
modifiers[index] = meta
#show("get_modifiers() modifiers="+modifiers.toSource());
modifiers
###*
# Process a key event: key pressed or key released.
# Figure out the keycode, keyname, modifiers, etc
# And send the event to the server.
###
processKeyEvent: (pressed, event) ->
# console.log pressed, event
# MSIE hack
if window.event
event = window.event
#show("@processKeyEvent("+pressed+", "+event+") keyCode="+event.keyCode+", charCode="+event.charCode+", which="+event.which);
keyname = ''
keycode = 0
if event.which
keycode = event.which
else
keycode = event.keyCode
# console.log 'keycode', keycode
if CHARCODE_TO_NAME[keycode]?
keyname = CHARCODE_TO_NAME[keycode]
DOM_KEY_LOCATION_RIGHT = 2
if keyname.match('_L$') and event.location == DOM_KEY_LOCATION_RIGHT
keyname = keyname.replace('_L', '_R')
# console.log 'keyname', keyname
modifiers = @get_modifiers(event)
if @caps_lock
modifiers.push 'lock'
keyval = keycode
str = String.fromCharCode(event.which)
group = 0
shift = modifiers.indexOf('shift') >= 0
if @caps_lock and shift or !@caps_lock and !shift
str = str.toLowerCase()
if @topwindow?
#show("win="+win.toSource()+", keycode="+keycode+", modifiers=["+modifiers+"], str="+str);
packet = [
'key-action'
@topwindow
keyname
pressed
modifiers
keyval
str
keycode
group
]
console.log 'packet', packet
@xpra.proto.Send.apply @xpra.proto, packet
return
onkeydown: (event) ->
@processKeyEvent true, event
false
onkeyup: (event) ->
@processKeyEvent false, event
false
###*
# This function is only used for figuring out the @caps_lock state!
# onkeyup and onkeydown give us the raw keycode,
# whereas here we get the keycode in lowercase/uppercase depending
# on the @caps_lock and shift state, which allows us to figure
# out @caps_lock state since we have shift state.
###
onkeypress: (event) ->
keycode = 0
if event.which
keycode = event.which
else
keycode = event.keyCode
modifiers = @get_modifiers(event)
### PITA: this only works for keypress event... ###
@caps_lock = false
shift = modifiers.indexOf('shift') >= 0
if keycode >= 97 and keycode <= 122 and shift
@caps_lock = true
else if keycode >= 65 and keycode <= 90 and !shift
@caps_lock = true
#show("@caps_lock="+@caps_lock);
false
window.oncontextmenu = (e) ->
#showCustomMenu();
false