|
| 1 | +package { |
| 2 | +import flash.display.Sprite; |
| 3 | +import flash.events.KeyboardEvent; |
| 4 | +import flash.events.MouseEvent; |
| 5 | +import flash.events.TextEvent; |
| 6 | +import flash.text.TextField; |
| 7 | +import flash.text.TextFormat; |
| 8 | +import flash.ui.Keyboard; |
| 9 | + |
| 10 | +[SWF(width="400", height="200")] |
| 11 | +public class Test extends Sprite { |
| 12 | + [Embed(source="NotoSans.ttf", fontName="Noto Sans", embedAsCFF="false", unicodeRange="U+0020-U+007E")] |
| 13 | + private var notoSans:Class; |
| 14 | + |
| 15 | + private var text:TextField; |
| 16 | + |
| 17 | + public function Test() { |
| 18 | + text = new TextField(); |
| 19 | + text.border = true; |
| 20 | + text.width = 400; |
| 21 | + text.height = 200; |
| 22 | + text.type = "input"; |
| 23 | + text.wordWrap = true; |
| 24 | + text.multiline = true; |
| 25 | + var tf = new TextFormat(); |
| 26 | + tf.font = "Noto Sans"; |
| 27 | + tf.size = 24; |
| 28 | + text.defaultTextFormat = tf; |
| 29 | + addChild(text); |
| 30 | + |
| 31 | + stage.focus = text; |
| 32 | + stage.addEventListener(TextEvent.TEXT_INPUT, textInput); |
| 33 | + stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown); |
| 34 | + stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); |
| 35 | + stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp); |
| 36 | + stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove); |
| 37 | + } |
| 38 | + |
| 39 | + private function keyPressedDown(event:KeyboardEvent):void { |
| 40 | + if (event.keyCode == Keyboard.ESCAPE) { |
| 41 | + trace("Selected: " + text.selectedText.replace(/\r/g, "\\n").replace(/\n/g, "\\n")); |
| 42 | + text.setSelection(0, 0); |
| 43 | + } else if (event.keyCode == Keyboard.NUMBER_1) { |
| 44 | + text.text = "word1_word2_word3"; |
| 45 | + } else if (event.keyCode == Keyboard.NUMBER_2) { |
| 46 | + text.text = "word1 word2 word3"; |
| 47 | + } else if (event.keyCode == Keyboard.NUMBER_3) { |
| 48 | + text.text = "word1 word2 word3 word4 word5 word6 word7 word8 word9\nword10 word11 word12"; |
| 49 | + } else if (event.keyCode == Keyboard.NUMBER_4) { |
| 50 | + text.selectable = false; |
| 51 | + } else if (event.keyCode == Keyboard.NUMBER_5) { |
| 52 | + text.selectable = true; |
| 53 | + } else if (event.keyCode == Keyboard.NUMBER_6) { |
| 54 | + text.text = "word1 word2"; |
| 55 | + } |
| 56 | + trace("{ \"type\": \"KeyDown\", \"key_code\": " + event.keyCode + " },") |
| 57 | + } |
| 58 | + |
| 59 | + private function mouseDown(event:MouseEvent):void { |
| 60 | + trace("{ \"type\": \"MouseDown\", \"pos\": [" + event.stageX + ", " + event.stageY + "], \"btn\": \"Left\" },"); |
| 61 | + } |
| 62 | + |
| 63 | + private function mouseUp(event:MouseEvent):void { |
| 64 | + trace("{ \"type\": \"MouseUp\", \"pos\": [" + event.stageX + ", " + event.stageY + "], \"btn\": \"Left\" },"); |
| 65 | + } |
| 66 | + |
| 67 | + private function mouseMove(event:MouseEvent):void { |
| 68 | + trace("{ \"type\": \"MouseMove\", \"pos\": [" + event.stageX + ", " + event.stageY + "] },"); |
| 69 | + } |
| 70 | + |
| 71 | + private function textInput(event:TextEvent):void { |
| 72 | + if (event.text.search(/[0-9]/g) != -1) { |
| 73 | + event.preventDefault(); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +} |
0 commit comments