Skip to content
This repository
Browse code

support for non-alpha characters for keyCode

  • Loading branch information...
commit fb97fe8577661d73ba715853ff39ef059ba2d30a 1 parent 399a8fb
authored March 10, 2012
20  spec/driver_spec.rb
@@ -1461,18 +1461,29 @@ def which_for(character)
1461 1461
 
1462 1462
     it "returns the charCode for the keypressed" do
1463 1463
       charCode_for("a").should == "97"
  1464
+      charCode_for("A").should == "65"
  1465
+      charCode_for("\r").should == "13"
  1466
+      charCode_for(",").should == "44"
  1467
+      charCode_for("<").should == "60"
  1468
+      charCode_for("0").should == "48"
1464 1469
     end
1465 1470
 
1466 1471
     it "returns the keyCode for the keypressed" do
1467 1472
       keyCode_for("a").should == "97"
1468 1473
       keyCode_for("A").should == "65"
1469 1474
       keyCode_for("\r").should == "13"
  1475
+      keyCode_for(",").should == "44"
  1476
+      keyCode_for("<").should == "60"
  1477
+      keyCode_for("0").should == "48"
1470 1478
     end
1471 1479
 
1472 1480
     it "returns the which for the keypressed" do
1473 1481
       which_for("a").should == "97"
1474 1482
       which_for("A").should == "65"
1475 1483
       which_for("\r").should == "13"
  1484
+      which_for(",").should == "44"
  1485
+      which_for("<").should == "60"
  1486
+      which_for("0").should == "48"
1476 1487
     end
1477 1488
   end
1478 1489
 
@@ -1481,18 +1492,27 @@ def which_for(character)
1481 1492
       charCode_for("a").should == "0"
1482 1493
       charCode_for("A").should == "0"
1483 1494
       charCode_for("\r").should == "0"
  1495
+      charCode_for(",").should == "0"
  1496
+      charCode_for("<").should == "0"
  1497
+      charCode_for("0").should == "0"
1484 1498
     end
1485 1499
 
1486 1500
     it "returns the keyCode for the event" do
1487 1501
       keyCode_for("a").should == "65"
1488 1502
       keyCode_for("A").should == "65"
1489 1503
       keyCode_for("\r").should == "13"
  1504
+      keyCode_for(",").should == "188"
  1505
+      keyCode_for("<").should == "188"
  1506
+      keyCode_for("0").should == "48"
1490 1507
     end
1491 1508
 
1492 1509
     it "returns the which for the event" do
1493 1510
       which_for("a").should == "65"
1494 1511
       which_for("A").should == "65"
1495 1512
       which_for("\r").should == "13"
  1513
+      which_for(",").should == "188"
  1514
+      which_for("<").should == "188"
  1515
+      which_for("0").should == "48"
1496 1516
     end
1497 1517
   end
1498 1518
 
45  src/capybara.js
@@ -163,6 +163,49 @@ Capybara = {
163 163
     return this.nodes[index].value;
164 164
   },
165 165
 
  166
+  characterToKeyCode: function(character) {
  167
+    var code = character.toUpperCase().charCodeAt(0);
  168
+    var specialKeys = {
  169
+      96: 192,  //`
  170
+      45: 189,  //-
  171
+      61: 187,  //=
  172
+      91: 219,  //[
  173
+      93: 221,  //]
  174
+      92: 220,  //\
  175
+      59: 186,  //;
  176
+      39: 222,  //'
  177
+      44: 188,  //,
  178
+      46: 190,  //.
  179
+      47: 191,  ///
  180
+      127: 46,  //delete
  181
+      126: 192, //~
  182
+      33: 49,   //!
  183
+      64: 50,   //@
  184
+      35: 51,   //#
  185
+      36: 52,   //$
  186
+      37: 53,   //%
  187
+      94: 54,   //^
  188
+      38: 55,   //&
  189
+      42: 56,   //*
  190
+      40: 57,   //(
  191
+      41: 48,   //)
  192
+      95: 189,  //_
  193
+      43: 187,  //+
  194
+      123: 219, //{
  195
+      125: 221, //}
  196
+      124: 220, //|
  197
+      58: 186,  //:
  198
+      34: 222,  //"
  199
+      60: 188,  //<
  200
+      62: 190,  //>
  201
+      63: 191   //?
  202
+    };
  203
+    if (specialKeys[code]) {
  204
+      code = specialKeys[code];
  205
+    }
  206
+    return code;
  207
+  },
  208
+
166 209
   set: function (index, value) {
167 210
     var length, maxLength, node, strindex, textTypes, type;
168 211
 
@@ -183,7 +226,7 @@ Capybara = {
183 226
       node.value = "";
184 227
       for (strindex = 0; strindex < length; strindex++) {
185 228
         node.value += value[strindex];
186  
-        var keyCode = value[strindex].toUpperCase().charCodeAt(0);
  229
+        var keyCode = this.characterToKeyCode(value[strindex]);
187 230
         this.keyupdown(index, "keydown", keyCode);
188 231
         this.keypress(index, false, false, false, false, value.charCodeAt(strindex), value.charCodeAt(strindex));
189 232
         this.keyupdown(index, "keyup", keyCode);

0 notes on commit fb97fe8

Please sign in to comment.
Something went wrong with that request. Please try again.