Skip to content

Commit f75c917

Browse files
author
Christopher Willis-Ford
committed
Revert "Merge pull request scratchfoundation#1341 from cwillisf/use-keycode-for-whenKeyPressed"
This reverts commit 10a60fa, reversing changes made to 23fddb0.
1 parent a489c76 commit f75c917

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/scratch/ScratchRuntime.as

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import flash.media.*;
3737
import flash.net.*;
3838
import flash.system.System;
3939
import flash.text.TextField;
40-
import flash.ui.Keyboard;
4140
import flash.utils.*;
4241

4342
import interpreter.*;
@@ -949,37 +948,31 @@ public class ScratchRuntime {
949948

950949
public function keyDown(evt:KeyboardEvent):void {
951950
shiftIsDown = evt.shiftKey;
952-
var ch:int = getCharCode(evt);
951+
var ch:int = evt.charCode;
952+
if (evt.charCode == 0) ch = mapArrowKey(evt.keyCode);
953+
if ((65 <= ch) && (ch <= 90)) ch += 32; // map A-Z to a-z
953954
if (!(evt.target is TextField)) startKeyHats(ch);
954955
if (ch < 128) keyIsDown[ch] = true;
955956
}
956957

957958
public function keyUp(evt:KeyboardEvent):void {
958959
shiftIsDown = evt.shiftKey;
959-
var ch:int = getCharCode(evt);
960+
var ch:int = evt.charCode;
961+
if (evt.charCode == 0) ch = mapArrowKey(evt.keyCode);
962+
if ((65 <= ch) && (ch <= 90)) ch += 32; // map A-Z to a-z
960963
if (ch < 128) keyIsDown[ch] = false;
961964
}
962965

963966
private function clearKeyDownArray():void {
964967
for (var i:int = 0; i < 128; i++) keyIsDown[i] = false;
965968
}
966969

967-
// Get an ASCII value for the key pressed:
968-
// - Latin letters will be normalized to lower-case
969-
// - Arrows will be mapped to ASCII/Unicode delimiters (this is a traditional hack in Scratch 2.0; see mapArrowKey)
970-
private static function getCharCode(evt:KeyboardEvent):int {
971-
var arrowCode:int = mapArrowKey(evt.keyCode);
972-
if (arrowCode) return arrowCode;
973-
return String.fromCharCode(evt.keyCode).toLowerCase().charCodeAt(0);
974-
}
975-
976-
// Map key codes for arrow keys to ASCII delimiters, and other key codes to zero.
977-
// See https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
978-
private static function mapArrowKey(keyCode:int):int {
979-
if (keyCode == Keyboard.LEFT) return 28; // file separator
980-
if (keyCode == Keyboard.UP) return 30; // group separator
981-
if (keyCode == Keyboard.RIGHT) return 29; // record separator
982-
if (keyCode == Keyboard.DOWN) return 31; // unit separator
970+
private function mapArrowKey(keyCode:int):int {
971+
// map key codes for arrow keys to ASCII, other key codes to zero
972+
if (keyCode == 37) return 28;
973+
if (keyCode == 38) return 30;
974+
if (keyCode == 39) return 29;
975+
if (keyCode == 40) return 31;
983976
return 0;
984977
}
985978

0 commit comments

Comments
 (0)