Permalink
Browse files

Issue #2 - Add key-recognition control to SKG for inputting Key to Bind

  • Loading branch information...
1 parent 0382d86 commit 66246a2f25ec1cdaf8bdff902f8daf8e6931f935 @jessewebb committed Mar 8, 2015
Showing with 133 additions and 38 deletions.
  1. +76 −38 data/bindable-keys.json
  2. +44 −0 js/controllers.js
  3. +13 −0 partials/single-key-generator.phtml
@@ -5,67 +5,83 @@
"keys": [
{
"name": "1 / End",
- "bind": "kp_end"
+ "bind": "kp_end",
+ "code": 97
},
{
"name": "2 / Down Arrow",
- "bind": "kp_downarrow"
+ "bind": "kp_downarrow",
+ "code": 98
},
{
"name": "3 / Page Down",
- "bind": "kp_pgdn"
+ "bind": "kp_pgdn",
+ "code": 99
},
{
"name": "4 / Left Arrow",
- "bind": "kp_leftarrow"
+ "bind": "kp_leftarrow",
+ "code": 100
},
{
"name": "5",
- "bind": "kp_5"
+ "bind": "kp_5",
+ "code": 101
},
{
"name": "6 / Right Arrow",
- "bind": "kp_rightarrow"
+ "bind": "kp_rightarrow",
+ "code": 102
},
{
"name": "7 / Home",
- "bind": "kp_home"
+ "bind": "kp_home",
+ "code": 103
},
{
"name": "8 / Up Arrow",
- "bind": "kp_uparrow"
+ "bind": "kp_uparrow",
+ "code": 104
},
{
"name": "9 / Page Up",
- "bind": "kp_pgup"
+ "bind": "kp_pgup",
+ "code": 105
},
{
"name": "0 / Insert",
- "bind": "kp_ins"
+ "bind": "kp_ins",
+ "code": 96
},
{
"name": ". / Delete",
- "bind": "kp_del"
+ "bind": "kp_del",
+ "code": 110
},
{
"name": "/ (Slash)",
- "bind": "kp_slash"
+ "bind": "kp_slash",
+ "code": 111
},
{
"name": "* (Multiply)",
- "bind": "kp_multiply"
+ "bind": "kp_multiply",
+ "code": 106
},
{
"name": "- (Minus)",
- "bind": "kp_minus"
+ "bind": "kp_minus",
+ "code": 109
},
{
"name": "+ (Plus)",
- "bind": "kp_plus"
+ "bind": "kp_plus",
+ "code": 107
},
{
"name": "Enter",
- "bind": "kp_enter"
+ "bind": "kp_enter",
+ "code": null
}
]
},
@@ -74,43 +90,53 @@
"keys": [
{
"name": "Insert",
- "bind": "ins"
+ "bind": "ins",
+ "code": 45
},
{
"name": "Delete",
- "bind": "del"
+ "bind": "del",
+ "code": 46
},
{
"name": "Home",
- "bind": "home"
+ "bind": "home",
+ "code": 36
},
{
"name": "End",
- "bind": "end"
+ "bind": "end",
+ "code": 35
},
{
"name": "Page Up",
- "bind": "pgup"
+ "bind": "pgup",
+ "code": 33
},
{
"name": "Page Down",
- "bind": "pgdn"
+ "bind": "pgdn",
+ "code": 34
},
{
"name": "Up Arrow",
- "bind": "uparrow"
+ "bind": "uparrow",
+ "code": 38
},
{
"name": "Left Arrow",
- "bind": "leftarrow"
+ "bind": "leftarrow",
+ "code": 37
},
{
"name": "Down Arrow",
- "bind": "downarrow"
+ "bind": "downarrow",
+ "code": 40
},
{
"name": "Right Arrow",
- "bind": "rightarrow"
+ "bind": "rightarrow",
+ "code": 39
}
]
},
@@ -119,51 +145,63 @@
"keys": [
{
"name": "F1",
- "bind": "f1"
+ "bind": "f1",
+ "code": 112
},
{
"name": "F2",
- "bind": "f2"
+ "bind": "f2",
+ "code": 112
},
{
"name": "F3",
- "bind": "f3"
+ "bind": "f3",
+ "code": 112
},
{
"name": "F4",
- "bind": "f4"
+ "bind": "f4",
+ "code": 112
},
{
"name": "F5",
- "bind": "f5"
+ "bind": "f5",
+ "code": 112
},
{
"name": "F6",
- "bind": "f6"
+ "bind": "f6",
+ "code": 112
},
{
"name": "F7",
- "bind": "f7"
+ "bind": "f7",
+ "code": 112
},
{
"name": "F8",
- "bind": "f8"
+ "bind": "f8",
+ "code": 112
},
{
"name": "F9",
- "bind": "f9"
+ "bind": "f9",
+ "code": 112
},
{
"name": "F10",
- "bind": "f10"
+ "bind": "f10",
+ "code": 112
},
{
"name": "F11",
- "bind": "f11"
+ "bind": "f11",
+ "code": 112
},
{
"name": "F12",
- "bind": "f12"
+ "bind": "f12",
+ "code": 112
}
]
}
View
@@ -22,8 +22,52 @@ buyndsControllers.controller('SingleKeyGenCtrl', ['$scope', '$route', '$window',
$scope.bindOptions = new buynds.BindOptions();
$scope.buyBind = '';
+ $scope.keyRecognitionActivated = false;
$scope.submitted = false;
+ var setupPointerLock = function (elem) {
+ elem.requestPointerLock = elem.requestPointerLock ||
+ elem.mozRequestPointerLock || elem.webkitRequestPointerLock;
+ elem.requestPointerLock();
+ };
+
+ var disablePointerLock = function (document) {
+ document.exitPointerLock = document.exitPointerLock ||
+ document.mozExitPointerLock || document.webkitExitPointerLock;
+ document.exitPointerLock();
+ };
+
+ var findBindableKeyByCode = function (keyCode) {
+ for (var i = 0; i < $scope.bindableKeys.keyGroups.length; i++) {
+ var keyGroup = $scope.bindableKeys.keyGroups[i];
+ for (var j = 0; j < keyGroup.keys.length; j++) {
+ var key = keyGroup.keys[j];
+ if (key.code == keyCode) return key;
+ }
+ }
+ };
+
+ $scope.activateKeyRecognition = function ($event) {
+ setupPointerLock($event.currentTarget);
+ jQuery($window.document).keydown($scope.registerKeyPress);
+ $scope.keyRecognitionActivated = true;
+ };
+
+ $scope.registerKeyPress = function (keyEvent) {
+ jQuery($window.document).unbind('keydown');
+ disablePointerLock($window.document);
+ var keyCode = keyEvent.which;
+ var bindableKey = findBindableKeyByCode(keyEvent.which);
+ if (bindableKey == null) {
+ alert('Unrecognized Key! (keyCode = ' + keyCode + ')');
+ } else {
+ $scope.bindOptions.keyToBind = bindableKey.bind;
+ }
+ $scope.keyRecognitionActivated = false;
+ keyEvent.preventDefault();
+ $scope.$apply(); // force refresh because this function executes asynchronously outside AngularJS scope
+ };
+
$scope.toggleGearSelection = function (gearBind) {
var idx = $scope.bindOptions.gear.indexOf(gearBind);
if (idx > -1) {
@@ -19,6 +19,19 @@
ng-selected="bindOptions.keyToBind == bindableKey.bind">{{ bindableKey.name }}</option>
</optgroup>
</select>
+ </div>
+ </div>
+ <div class="form-group" ng-class="{ 'has-error': submitted && skgForm.skgKeySelect.$invalid }">
+ <div class="col-sm-offset-3 col-sm-9">
+ <div class="input-group">
+ <div class="btn input-group-addon"
+ ng-click="activateKeyRecognition($event)"
+ ng-class="{ 'btn-default': !keyRecognitionActivated, 'btn-info': keyRecognitionActivated }">
+ Select Key <span class="glyphicon glyphicon-edit"></span>
+ </div>
+ <div class="form-control" disabled><code ng-show="bindOptions.keyToBind">{{ bindOptions.keyToBind }}</code></div>
+ <span class="glyphicon glyphicon-remove form-control-feedback" ng-show="submitted && skgForm.skgKeySelect.$invalid"></span>
+ </div>
<p class="help-block" ng-show="submitted && skgForm.skgKeySelect.$invalid">* You must select a key to bind!</p>
</div>
</div>

0 comments on commit 66246a2

Please sign in to comment.