Skip to content

Commit e2e7ad0

Browse files
committed
avm2: Implement flash.ui.Keyboard
Add `Keyboard` key code constants and stub out other properties.
1 parent 1cb3d38 commit e2e7ad0

File tree

1 file changed

+350
-1
lines changed

1 file changed

+350
-1
lines changed

core/src/avm2/globals/flash/ui/keyboard.rs

Lines changed: 350 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
33
use crate::avm2::activation::Activation;
44
use crate::avm2::class::{Class, ClassAttributes};
5-
use crate::avm2::method::Method;
5+
use crate::avm2::method::{Method, NativeMethodImpl};
66
use crate::avm2::names::{Namespace, QName};
77
use crate::avm2::value::Value;
88
use crate::avm2::{Error, Object};
9+
use crate::events::KeyCode;
10+
use crate::string::AvmString;
911
use gc_arena::{GcCell, MutationContext};
1012

1113
/// Implements `flash.ui.Keyboard`'s class constructor.
@@ -38,5 +40,352 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>
3840
let mut write = class.write(mc);
3941
write.set_attributes(ClassAttributes::FINAL | ClassAttributes::SEALED);
4042

43+
const STRING_CONSTANTS: &[(&str, &str)] = &[
44+
("KEYNAME_UPARROW", "Up"),
45+
("KEYNAME_DOWNARROW", "Down"),
46+
("KEYNAME_LEFTARROW", "Left"),
47+
("KEYNAME_RIGHTARROW", "Right"),
48+
("KEYNAME_F1", "F1"),
49+
("KEYNAME_F2", "F2"),
50+
("KEYNAME_F3", "F3"),
51+
("KEYNAME_F4", "F4"),
52+
("KEYNAME_F5", "F5"),
53+
("KEYNAME_F6", "F6"),
54+
("KEYNAME_F7", "F7"),
55+
("KEYNAME_F8", "F8"),
56+
("KEYNAME_F9", "F9"),
57+
("KEYNAME_F10", "F10"),
58+
("KEYNAME_F11", "F11"),
59+
("KEYNAME_F12", "F12"),
60+
("KEYNAME_F13", "F13"),
61+
("KEYNAME_F14", "F14"),
62+
("KEYNAME_F15", "F15"),
63+
("KEYNAME_F16", "F16"),
64+
("KEYNAME_F17", "F17"),
65+
("KEYNAME_F18", "F18"),
66+
("KEYNAME_F19", "F19"),
67+
("KEYNAME_F20", "F20"),
68+
("KEYNAME_F21", "F21"),
69+
("KEYNAME_F22", "F22"),
70+
("KEYNAME_F23", "F23"),
71+
("KEYNAME_F24", "F24"),
72+
("KEYNAME_F25", "F25"),
73+
("KEYNAME_F26", "F26"),
74+
("KEYNAME_F27", "F27"),
75+
("KEYNAME_F28", "F28"),
76+
("KEYNAME_F29", "F29"),
77+
("KEYNAME_F30", "F30"),
78+
("KEYNAME_F31", "F31"),
79+
("KEYNAME_F32", "F32"),
80+
("KEYNAME_F33", "F33"),
81+
("KEYNAME_F34", "F34"),
82+
("KEYNAME_F35", "F35"),
83+
("KEYNAME_INSERT", "Insert"),
84+
("KEYNAME_DELETE", "Delete"),
85+
("KEYNAME_HOME", "Home"),
86+
("KEYNAME_BEGIN", "Begin"),
87+
("KEYNAME_END", "End"),
88+
("KEYNAME_PAGEUP", "PgUp"),
89+
("KEYNAME_PAGEDOWN", "PgDn"),
90+
("KEYNAME_PRINTSCREEN", "PrntScrn"),
91+
("KEYNAME_SCROLLLOCK", "ScrlLck"),
92+
("KEYNAME_PAUSE", "Pause"),
93+
("KEYNAME_SYSREQ", "SysReq"),
94+
("KEYNAME_BREAK", "Break"),
95+
("KEYNAME_RESET", "Reset"),
96+
("KEYNAME_STOP", "Stop"),
97+
("KEYNAME_MENU", "Menu"),
98+
("KEYNAME_USER", "User"),
99+
("KEYNAME_SYSTEM", "Sys"),
100+
("KEYNAME_PRINT", "Print"),
101+
("KEYNAME_CLEARLINE", "ClrLn"),
102+
("KEYNAME_CLEARDISPLAY", "ClrDsp"),
103+
("KEYNAME_INSERTLINE", "InsLn"),
104+
("KEYNAME_DELETELINE", "DelLn"),
105+
("KEYNAME_INSERTCHAR", "InsChr"),
106+
("KEYNAME_DELETECHAR", "DelChr"),
107+
("KEYNAME_PREV", "Prev"),
108+
("KEYNAME_NEXT", "Next"),
109+
("KEYNAME_SELECT", "Select"),
110+
("KEYNAME_EXECUTE", "Exec"),
111+
("KEYNAME_UNDO", "Undo"),
112+
("KEYNAME_REDO", "Redo"),
113+
("KEYNAME_FIND", "Find"),
114+
("KEYNAME_HELP", "Help"),
115+
("KEYNAME_MODESWITCH", "ModeSw"),
116+
("KEYNAME_PLAYPAUSE", "PlayPause"),
117+
("STRING_UPARROW", "\u{f700}"),
118+
("STRING_DOWNARROW", "\u{f701}"),
119+
("STRING_LEFTARROW", "\u{f702}"),
120+
("STRING_RIGHTARROW", "\u{f703}"),
121+
("STRING_F1", "\u{f704}"),
122+
("STRING_F2", "\u{f705}"),
123+
("STRING_F3", "\u{f706}"),
124+
("STRING_F4", "\u{f707}"),
125+
("STRING_F5", "\u{f708}"),
126+
("STRING_F6", "\u{f709}"),
127+
("STRING_F7", "\u{f70a}"),
128+
("STRING_F8", "\u{f70b}"),
129+
("STRING_F9", "\u{f70c}"),
130+
("STRING_F10", "\u{f70d}"),
131+
("STRING_F11", "\u{f70e}"),
132+
("STRING_F12", "\u{f70f}"),
133+
("STRING_F13", "\u{f710}"),
134+
("STRING_F14", "\u{f711}"),
135+
("STRING_F15", "\u{f712}"),
136+
("STRING_F16", "\u{f713}"),
137+
("STRING_F17", "\u{f714}"),
138+
("STRING_F18", "\u{f715}"),
139+
("STRING_F19", "\u{f716}"),
140+
("STRING_F20", "\u{f717}"),
141+
("STRING_F21", "\u{f718}"),
142+
("STRING_F22", "\u{f719}"),
143+
("STRING_F23", "\u{f71a}"),
144+
("STRING_F24", "\u{f71b}"),
145+
("STRING_F25", "\u{f71c}"),
146+
("STRING_F26", "\u{f71d}"),
147+
("STRING_F27", "\u{f71e}"),
148+
("STRING_F28", "\u{f71f}"),
149+
("STRING_F29", "\u{f720}"),
150+
("STRING_F30", "\u{f721}"),
151+
("STRING_F31", "\u{f722}"),
152+
("STRING_F32", "\u{f723}"),
153+
("STRING_F33", "\u{f724}"),
154+
("STRING_F34", "\u{f725}"),
155+
("STRING_F35", "\u{f726}"),
156+
("STRING_INSERT", "\u{f727}"),
157+
("STRING_DELETE", "\u{f728}"),
158+
("STRING_HOME", "\u{f729}"),
159+
("STRING_BEGIN", "\u{f72a}"),
160+
("STRING_END", "\u{f72b}"),
161+
("STRING_PAGEUP", "\u{f72c}"),
162+
("STRING_PAGEDOWN", "\u{f72d}"),
163+
("STRING_PRINTSCREEN", "\u{f72e}"),
164+
("STRING_SCROLLLOCK", "\u{f72f}"),
165+
("STRING_PAUSE", "\u{f730}"),
166+
("STRING_SYSREQ", "\u{f731}"),
167+
("STRING_BREAK", "\u{f732}"),
168+
("STRING_RESET", "\u{f733}"),
169+
("STRING_STOP", "\u{f734}"),
170+
("STRING_MENU", "\u{f735}"),
171+
("STRING_USER", "\u{f736}"),
172+
("STRING_SYSTEM", "\u{f737}"),
173+
("STRING_PRINT", "\u{f738}"),
174+
("STRING_CLEARLINE", "\u{f739}"),
175+
("STRING_CLEARDISPLAY", "\u{f73a}"),
176+
("STRING_INSERTLINE", "\u{f73b}"),
177+
("STRING_DELETELINE", "\u{f73c}"),
178+
("STRING_INSERTCHAR", "\u{f73d}"),
179+
("STRING_DELETECHAR", "\u{f73e}"),
180+
("STRING_PREV", "\u{f73f}"),
181+
("STRING_NEXT", "\u{f740}"),
182+
("STRING_SELECT", "\u{f741}"),
183+
("STRING_EXECUTE", "\u{f742}"),
184+
("STRING_UNDO", "\u{f743}"),
185+
("STRING_REDO", "\u{f744}"),
186+
("STRING_FIND", "\u{f745}"),
187+
("STRING_HELP", "\u{f746}"),
188+
("STRING_MODESWITCH", "\u{f747}"),
189+
];
190+
write.define_public_constant_string_class_traits(STRING_CONSTANTS);
191+
192+
// TODO: Add more of these constants to KeyCode enum.
193+
const UINT_CONSTANTS: &[(&str, u32)] = &[
194+
("NUMBER_0", KeyCode::Key0 as u32),
195+
("NUMBER_1", KeyCode::Key1 as u32),
196+
("NUMBER_2", KeyCode::Key2 as u32),
197+
("NUMBER_3", KeyCode::Key3 as u32),
198+
("NUMBER_4", KeyCode::Key4 as u32),
199+
("NUMBER_5", KeyCode::Key5 as u32),
200+
("NUMBER_6", KeyCode::Key6 as u32),
201+
("NUMBER_7", KeyCode::Key7 as u32),
202+
("NUMBER_8", KeyCode::Key8 as u32),
203+
("NUMBER_9", KeyCode::Key9 as u32),
204+
("A", KeyCode::A as u32),
205+
("B", KeyCode::B as u32),
206+
("C", KeyCode::C as u32),
207+
("D", KeyCode::D as u32),
208+
("E", KeyCode::E as u32),
209+
("F", KeyCode::F as u32),
210+
("G", KeyCode::G as u32),
211+
("H", KeyCode::H as u32),
212+
("I", KeyCode::I as u32),
213+
("J", KeyCode::J as u32),
214+
("K", KeyCode::K as u32),
215+
("L", KeyCode::L as u32),
216+
("M", KeyCode::M as u32),
217+
("N", KeyCode::N as u32),
218+
("O", KeyCode::O as u32),
219+
("P", KeyCode::P as u32),
220+
("Q", KeyCode::Q as u32),
221+
("R", KeyCode::R as u32),
222+
("S", KeyCode::S as u32),
223+
("T", KeyCode::T as u32),
224+
("U", KeyCode::U as u32),
225+
("V", KeyCode::V as u32),
226+
("W", KeyCode::W as u32),
227+
("X", KeyCode::X as u32),
228+
("Y", KeyCode::Y as u32),
229+
("Z", KeyCode::Z as u32),
230+
("SEMICOLON", KeyCode::Semicolon as u32),
231+
("EQUAL", KeyCode::Equals as u32),
232+
("COMMA", KeyCode::Comma as u32),
233+
("MINUS", KeyCode::Minus as u32),
234+
("PERIOD", KeyCode::Period as u32),
235+
("SLASH", KeyCode::Slash as u32),
236+
("BACKQUOTE", KeyCode::Grave as u32),
237+
("LEFTBRACKET", KeyCode::LBracket as u32),
238+
("BACKSLASH", KeyCode::Backslash as u32),
239+
("RIGHTBRACKET", KeyCode::RBracket as u32),
240+
("QUOTE", KeyCode::Apostrophe as u32),
241+
("ALTERNATE", KeyCode::Alt as u32),
242+
("BACKSPACE", KeyCode::Backspace as u32),
243+
("CAPS_LOCK", KeyCode::CapsLock as u32),
244+
("COMMAND", KeyCode::Command as u32),
245+
("CONTROL", KeyCode::Control as u32),
246+
("DELETE", KeyCode::Delete as u32),
247+
("DOWN", KeyCode::Down as u32),
248+
("END", KeyCode::End as u32),
249+
("ENTER", KeyCode::Return as u32),
250+
("ESCAPE", KeyCode::Escape as u32),
251+
("F1", KeyCode::F1 as u32),
252+
("F2", KeyCode::F2 as u32),
253+
("F3", KeyCode::F3 as u32),
254+
("F4", KeyCode::F4 as u32),
255+
("F5", KeyCode::F5 as u32),
256+
("F6", KeyCode::F6 as u32),
257+
("F7", KeyCode::F7 as u32),
258+
("F8", KeyCode::F8 as u32),
259+
("F9", KeyCode::F9 as u32),
260+
("F10", KeyCode::F10 as u32),
261+
("F11", KeyCode::F11 as u32),
262+
("F12", KeyCode::F12 as u32),
263+
("F13", KeyCode::F13 as u32),
264+
("F14", KeyCode::F14 as u32),
265+
("F15", KeyCode::F15 as u32),
266+
("HOME", KeyCode::Home as u32),
267+
("INSERT", KeyCode::Insert as u32),
268+
("LEFT", KeyCode::Left as u32),
269+
("NUMPAD", KeyCode::Numpad as u32), // Numpad meta key, not Num Lock
270+
("NUMPAD_0", KeyCode::Numpad0 as u32),
271+
("NUMPAD_1", KeyCode::Numpad1 as u32),
272+
("NUMPAD_2", KeyCode::Numpad2 as u32),
273+
("NUMPAD_3", KeyCode::Numpad3 as u32),
274+
("NUMPAD_4", KeyCode::Numpad4 as u32),
275+
("NUMPAD_5", KeyCode::Numpad5 as u32),
276+
("NUMPAD_6", KeyCode::Numpad6 as u32),
277+
("NUMPAD_7", KeyCode::Numpad7 as u32),
278+
("NUMPAD_8", KeyCode::Numpad8 as u32),
279+
("NUMPAD_9", KeyCode::Numpad9 as u32),
280+
("NUMPAD_ADD", KeyCode::Plus as u32),
281+
("NUMPAD_DECIMAL", KeyCode::NumpadPeriod as u32),
282+
("NUMPAD_DIVIDE", KeyCode::NumpadSlash as u32),
283+
("NUMPAD_ENTER", KeyCode::NumpadEnter as u32),
284+
("NUMPAD_MULTIPLY", KeyCode::Multiply as u32),
285+
("NUMPAD_SUBTRACT", KeyCode::NumpadMinus as u32),
286+
("PAGE_DOWN", KeyCode::PgDown as u32),
287+
("PAGE_UP", KeyCode::PgUp as u32),
288+
("RIGHT", KeyCode::Right as u32),
289+
("SHIFT", KeyCode::Shift as u32),
290+
("SPACE", KeyCode::Space as u32),
291+
("TAB", KeyCode::Tab as u32),
292+
("UP", KeyCode::Up as u32),
293+
// Meta keys
294+
("RED", 16777216),
295+
("GREEN", 16777217),
296+
("YELLOW", 16777218),
297+
("BLUE", 16777219),
298+
("CHANNEL_UP", 16777220),
299+
("CHANNEL_DOWN", 16777221),
300+
("RECORD", 16777222),
301+
("PLAY", 16777223),
302+
("PAUSE", 16777224),
303+
("STOP", 16777225),
304+
("FAST_FORWARD", 16777226),
305+
("REWIND", 16777227),
306+
("SKIP_FORWARD", 16777228),
307+
("SKIP_BACKWARD", 16777229),
308+
("NEXT", 16777230),
309+
("PREVIOUS", 16777231),
310+
("LIVE", 16777232),
311+
("LAST", 16777233),
312+
("MENU", 16777234),
313+
("INFO", 16777235),
314+
("GUIDE", 16777236),
315+
("EXIT", 16777237),
316+
("BACK", 16777238),
317+
("AUDIO", 16777239),
318+
("SUBTITLE", 16777240),
319+
("DVR", 16777241),
320+
("VOD", 16777242),
321+
("INPUT", 16777243),
322+
("SETUP", 16777244),
323+
("HELP", 16777245),
324+
("MASTER_SHELL", 16777246),
325+
("SEARCH", 16777247),
326+
("PLAY_PAUSE", 16777248),
327+
];
328+
write.define_public_constant_uint_class_traits(UINT_CONSTANTS);
329+
330+
const PUBLIC_INSTANCE_PROPERTIES: &[(
331+
&str,
332+
Option<NativeMethodImpl>,
333+
Option<NativeMethodImpl>,
334+
)] = &[
335+
("capsLock", Some(caps_lock), None),
336+
("hasVirtualKeyboard", Some(has_virtual_keyboard), None),
337+
("numLock", Some(num_lock), None),
338+
("physicalKeyboardType", Some(physical_keyboard_type), None),
339+
];
340+
write.define_public_builtin_instance_properties(mc, PUBLIC_INSTANCE_PROPERTIES);
341+
342+
const PUBLIC_CLASS_METHODS: &[(&str, NativeMethodImpl)] = &[("isAccessible", is_accessible)];
343+
write.define_public_builtin_class_methods(mc, PUBLIC_CLASS_METHODS);
344+
41345
class
42346
}
347+
348+
fn caps_lock<'gc>(
349+
_activation: &mut Activation<'_, 'gc, '_>,
350+
_this: Option<Object<'gc>>,
351+
_args: &[Value<'gc>],
352+
) -> Result<Value<'gc>, Error> {
353+
log::warn!("Keyboard.capsLock: not yet implemented");
354+
Ok(false.into())
355+
}
356+
357+
fn has_virtual_keyboard<'gc>(
358+
_activation: &mut Activation<'_, 'gc, '_>,
359+
_this: Option<Object<'gc>>,
360+
_args: &[Value<'gc>],
361+
) -> Result<Value<'gc>, Error> {
362+
log::warn!("Keyboard.hasVirtualKeyboard: not yet implemented");
363+
Ok(false.into())
364+
}
365+
366+
fn num_lock<'gc>(
367+
_activation: &mut Activation<'_, 'gc, '_>,
368+
_this: Option<Object<'gc>>,
369+
_args: &[Value<'gc>],
370+
) -> Result<Value<'gc>, Error> {
371+
log::warn!("Keyboard.numLock: not yet implemented");
372+
Ok(false.into())
373+
}
374+
375+
fn physical_keyboard_type<'gc>(
376+
activation: &mut Activation<'_, 'gc, '_>,
377+
_this: Option<Object<'gc>>,
378+
_args: &[Value<'gc>],
379+
) -> Result<Value<'gc>, Error> {
380+
log::warn!("Keyboard.physicalKeyboardType: not yet implemented");
381+
Ok(AvmString::new_utf8(activation.context.gc_context, "alphanumeric").into())
382+
}
383+
384+
fn is_accessible<'gc>(
385+
_activation: &mut Activation<'_, 'gc, '_>,
386+
_this: Option<Object<'gc>>,
387+
_args: &[Value<'gc>],
388+
) -> Result<Value<'gc>, Error> {
389+
log::warn!("Keyboard.isAccessible: not yet implemented");
390+
Ok(true.into())
391+
}

0 commit comments

Comments
 (0)