Giter Club home page Giter Club logo

keypress's Introduction

Keypress

Version 2.1.5

Keypress is a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please visit http://dmauro.github.io/Keypress/

If you're using Keypress with Meteor, see the Meteor notes.

Copyright 2016 David Mauro
released under the Apache License, version 2.0

What's new

2.1.5

2.1.4

2.1.3

2.1.2

  • Added some simple jQuery proofing so you can pass in the result of a jQuery selector into the Listener's constructor. Thanks to mallocator for the request. Issue #89
  • Changed the default behavior of how sequence combos behavior. Most people probably would have considered this a bug, so I'm not considering this API breaking. Thanks to ronnyek for pointing the problem out in Issue #68.
  • Bower file cleaned up thanks to kkirsche. Pull request #97
  • Keys in FF/Gecko - and = fixed thanks to deanputney. Pull request #95

2.1.1

2.1.0

2.0.3

  • Fixed a bug unregistering combos using arrays of keys
  • Added ie8 compatibility shim. Thanks to barrkel. Issue #41
  • Fixed a bug targetting the semicolon key in Firefox. Thanks to mikekuehn.
  • Added commonJS module support. Issue #45

2.0.2

  • Fixed a bug that prevented combos from unregistering, and updated the docs for how to unregister properly. Thanks to pelly and g00fy-. Issue # 34.
  • Added AMD support. Issue #37.

2.0.1

  • Fixed a big ole bug with meta/cmd combos. Thanks to lexey111. Issue #29.
  • Fixed a bug with the Windows key being released on Windows systems. Issue #27.

2.0.0

  • Keypress now has a listener class that must be instantiated. The functions that were previously in the global window.keypress object are now public methods of the window.keypress.Listener class.
  • Each instance of a Keypress listener can be bound to a DOM element by passing in the element to the listener's constructor.
  • Combos now default to being ordered (the property is now called is_unordered and is false by default).
  • Combos' handlers preventDefault unless the handler returns true.
  • The "combo" public method is now called "simple_combo".
  • The basic combo helpers for simple, counting and sequence combos no longer have a third prevent_default optional parameter.
  • Debugging console logs can be enabled by setting keypress.debug to true.
  • All key event callbacks send a third argument specifying whether the event is firing again automatically because the key has remained pressed down.

1.0.9

  • Fix escape key bug. Issue #17.
  • Fix unregister bug. Issue #24.

1.0.8

  • Ensure that on_release is called for all combos, not just counting combos.
  • Fix bug that was causing is_ordered to be ignored.
  • Fixed an edge case for a counting combo's count not being reset.
  • Improve how key events are bound

1.0.7

  • Fixed combo matching to prevent performance issues as more keys are pressed.

1.0.6

  • Fixed a bug with exclusive combos not properly excluding each other in some cases.
  • Feature added to allow for combos that do not fire if unrelated keys are also pressed ("is_solitary" boolean).

TODO

  • Put negative edge in sequences.
  • See if we can do away with keyup_fired and count properties.

keypress's People

Contributors

ayebear avatar barrkel avatar beauwest avatar dandv avatar deanputney avatar dmauro avatar dranov avatar jetwitaussi avatar juleskuehn avatar kkirsche avatar kvantetore avatar mithgol avatar pelly avatar richard77 avatar smblott-github avatar smerickson avatar sorrycc avatar th3fallen avatar valera-rozuvan avatar zwalker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keypress's Issues

F1-F12 keys

I've tried to use f4, f_2, etc. It does not recognize it.
With monkey patching i've added those keys into _keycode_dictionary. It seems, that it worked.

If it not my mistake, please add this functionality.

Also have bugs with decimal at numpad detection after a switching keyboard layout from english to russian.

When i use hotkey num_7, i.e., it fires when i use non-existant hotkey, i.e. ctrl num_7.

Meta key bug?

Hello,

I am starting with Keypress, and so I tried pretty simple thing:

    listener.register_combo({
        "keys": "meta s",
        "prevent_default": true,
        "on_keydown": function () {
            $("input[id*='form-submit']").click();
            console.log('Keypress: meta s');
        }
    });

I was surprised, when I was unable to write letter "S" on the webpage, not even "s", so I added log about being pressed, it is fired correctly, when I press CTRL+S, but does not fire for SHIFT+S (as it should not), but still I am unable to write letters "s" and "S", can you help me or am I doing something wrong?

Chrome 39.0.2171.99 m
Windows 7

Esc key?

Is there a way to bind the esc key?

"meta + S" called twice when `is_sequence` was set true

Keydown events are always called twice on my demo.
I found that if I didn't set is_sequence, things go right, but both meta s and s meta would callback.
So here's my code (with keypress.js compiled with coffee 1.6.2):

my_combos = [
  {
    "keys"      : "meta s"
    "is_exclusive"  : true
    "is_sequence"   : true
    "on_keydown"  : ->
      console.log("You pressed shift and s together.")
      event.preventDefault()
    "this"      : @
  }
]
keypress.register_many(my_combos)

In the _key_down function, _fire was triggered twice,
first time at: https://github.com/dmauro/Keypress/blob/master/keypress.coffee#L314
second time at: https://github.com/dmauro/Keypress/blob/master/keypress.coffee#L335
I haven't figured out how it happens so far.

Should I be able to capture fkeys

First of all, I'm questioning whether doing anything with fkeys is a smart idea in the first place...

But I didnt see fkeys in your little main page demo. Should I be able to capture keys?

IE single key press alongside meta combination

I'm seeing unexpected behaviour in IE, where I have a single key as a shortcut, but also have another shortcut that uses the single key as well as the meta key. On other browsers it seems fine (fires both events separately), but on IE it is firing both events, the combination first and then the single key. I'm testing on a Windows 8 virtual machine, with IE10.

im getting "TypeError: target is null" on line 111 in the keypress.js file

when running this code:
var monsterPos = [0];
var monsterList= [0];
var singleMonster = [0];
var frame = 0;
var isAttacking = false;
var listener = new window.keypress.Listener();
// var l = 1;

$(document).ready(function()
{
setInterval(moveRight3, 100);

//document.addEventListener("keydown", attack1);
//document.addEventListener("keyup", function(){isAttacking=false;});
var monsterList = document.getElementsByClassName('monster');
var lenght = monsterList.length
for (var i = 0 ; i < length; i++)
{
    singleMonster = getPosition(monsterList[i]);
    monsterPos[monsterPos.length] = singleMonster.x;
}
// $(".monster").each(singleMonster = findPos(this);
// monsterPos[monsterPos.length] = singleMonster[0];
// monsterList[monsterList.length] = l; l++;);

});

function getPosition(element) {
var xPosition = 0;
var yPosition = 0;

while(element) {
    xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
    yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
    element = element.offsetParent;
}
return { x: xPosition, y: yPosition };

}

function monsterLister(element)//currently not in use
{
singleMonster = getPosition($(this));
monsterPos[monsterPos.length] = singleMonster.x;
monsterList[monsterList.length] = l;
l++;
}

function moveRight3()
{
frame = (frame+1)%4;
if (!isAttacking) {$("#hero").attr("src","move"+(frame+1)+".png");}
$('.monster').each(function(index, img){$(this).css("left", (parseInt($(this).css("left")) - 40))});
}

function attack1(event)
{
if ((($(this)).keyCode) == 65){
isAttacking = true;
for (q=0, len = monsterPos.length; len > q; q++){
if (400 > monsterPos[q] > 560){
$("monsterList["+q+"]").css(opacity, 0);
}}
$("#hero").attr("src","attack.png");
console.log("attack");

}}

listener.register_combo({
"keys" : "a",
"on_keydown" : function(){$("#hero").attr("src","attack.png");
isAttacking = true;
for (q=0, len = monsterPos.length; len > q; q++){
if (400 > monsterPos[q] > 560){
$("monsterList["+q+"]").css(opacity, 0);
}
}
},
on_keyup : function(){isAttacking = false;}
});

keypres inbetween time control

i dunno where to ask thats why raised this as an issue sorry..below is the seq i am planning to use

keypress.sequence_combo("backspace backspace", function() {
// lives = 120;
alert("You've pressed this bkspc.");
}, true);

the issue i am facing is..

when i press backspace very contionus for 2 times it works as expected but when i delay the press it does not work,,

is there a way where we can control this intermediate keypress control

Regards
Venkat.S

Version number

Hello,

There is no way to find the version number of your current release, and by the way no way to know if we should update our scripts when an update will be available.

The version number could be added as a very small comment on top of the minified js, or in the filename, like "keypress.1.0.js"

Regards,
Geoffray

is there a way to capture a key combo?

I'm wondering if there's a way to do something like

listener.captureNextCombo(function(combo){
  console.log ('captured combo:', combo);
});

which would print something like:

captured combo: cmd-s

Package name on Bower?

As bower register gives back Duplicated package, I found this package:

itg_twig.js 
A keyboard input capturing utility in which any key can be a modifier key.

dmauro  9 months ago    8 days ago  41  264

I suppose we just use keypress or Keypress. The current one looks strange.

Press state bug

Press win+shift button. Then release win button. Then release shift button. But win button show as pressed, still.

is_counting + is_sequence?

code below:

{
          "keys"          : "up up down down left right left right b a enter",
          "is_exclusive"  : true,
          "is_counting"   : true,
          "is_sequence"   : true,
          "is_ordered"    : true,
          "on_keydown"    : function(e, count) {
            switch (count) {
              case 1:
                console.log('Want 30 lives? Please try again :)');
                break;
              case 2:
                console.log('No luck still. One more time? :D');
                break;
              case 3:
              default:
                console.log('Tried 3 times already.');
                break;
            }
            return false;
          }
}

I can't get the count of the combo. Am I missing something here?

Thanks for the great work of this library, save me a lot of time :)

key_up not fired if focus changes

So I have a bit of a weird issue that took me forever to track down. The use case is this:

  1. I'm focused on an element that has hotkeys, in this case "enter" to select the item.
  2. When the item is selected, a new element is created that takes the focus. (newElement.focus())
  3. When complete with the new element, we close it with Escape.
  4. Focus comes back to the original element.

This works great, except in one circumstance. If the new element takes focus before the keyup event happens, the Keypress._keys_down array still shows that there is a key pressed! This causes issues for any other hotkeys that have is_solitary set to true.

Clear as mud?

The solution that I have implemented is, when the original element gets a regular blur event. I call listener.blur_event() which clears the array for when I come back to it.

If you need me to clarify further, or if there are any questions, please let me know!

on_keydown doesn't fire up if key is in 2 combos

Sorry for the title, don't know how to resume this in a single phrase.

I've got this code: (just modified a bit from the site's example)

var my_scope = this;
var my_combos = listener.register_many([
    {
        "keys"          : "shift s",
        "is_exclusive"  : true,
        "on_keydown"    : function() {
            console.log("You pressed shift and s together.");
        },
        "on_keyup"      : function(e) {
            console.log("And now you've released one of the keys.");
        },
        "this"          : my_scope
    },
    {
        "keys"          : "s",
        "is_exclusive"  : true,
        "on_keydown"    : function() {
            console.log("You pressed s.");
        },
        "on_keyup"      : function(event) {
          console.log("You released s.");
        },
        "this"          : my_scope
    }
]);

I use the is_exclusive feature as the site describes, everything works fine, except that the on_keydown for the single key (here, "s") doesn't fire up.

Example text typo

Please, replace "var listener = window.keypress.Listener();" to "var listener = new window.keypress.Listener();" on your site.

I had an interesting five minutes to understand why it does not work when I copied it from your example :)

Various

@dmauro :

  1. Include licence file in repo
  2. Include licence info in js file
  3. Update scripts and page to reflect current time
  4. Possibly provide an example with Bootstrap 3

Thanks

Browser Support

I can't find much in the documentation or anywhere else as to what browsers receive full support with this library. I note mention of an IE8 shim in the commit logs - is this an IE8+/Chrome/FF/Safari/Opera job, or are earlier IEs supported?

Support for different keyboard layouts

I see that the library works very well when using US or UK keyboard layouts - Great Job.

However there are a few issues when using a French keyboard (the only non English language I have tried).
For example, on a french keyboard, to get numbers, you have to use shift where the symbols are non shifted. The library detects the opposite way (The same as in English based languages)

Also when pressing the symbol keys (the ones between alphabet and the enter key), the wrong symbol is detected.
Is this a limitation or is it technically possible to handle these cases?

IE8 compatibility

I have this warning on IE8
'keyCode' has null value or is not an object
at keypress.js line 10 char 370
(original avert is in french)

in IE, it seems that onkeydown does not pass event in callback function ( e = 'undefined')
at line 488

"meta /" doesn't work on windows

keypress.combo('meta /', function(event) {
  // show help dialog ...
});

The problem on windows is if we holding "ctrl" then press down "/", it won't work. But reversely it works.

_bug_catcher

MacOS 10.9 (MBP Late 2013)

When I use "meta c" / "meta v" combinations I have error alert about unknown _receive_input" error in Listener.prototype._bug_catcher().

So I replaced [line 141]

return _receive_input(e, false);

by

return this._receive_input(e, false);

and it works now.

` a + any other key - Doesn't work

I am noticing that a three key combos using ` a 1 (and many other strange combinations of three key combo's using Tilda with numbers and letters) do not work. Any Ideas? I suspect it is my microsoft natural keyboard. Please close if you cannot replicate.

Thank you

Restricting input to specific DOM elements

Is there a way to specify the root DOM element for which all shortcuts apply? There are cases where the same shortcut can trigger different callbacks depending on which DOM element is focused for the keyboard input. Conversely, if there's a way we can also namespace the shortcut definitions, that will also solve this issue.

ctrl+s doesn't work any more

{
      "keys": "meta s",
      "is_exclusive": true,
      "is_ordered": true,
      "on_keydown": function(e) {
        // it don't trigger ?
        e.preventDefault();
        return false;
      },
      "on_keyup": function(e) {
       // it's ok
        e.preventDefault();
        return false;
      }
    }

when i use ctrl+s under windows , "on_keydown" function doesn't run. But it work well under mac.

Up to 6 keys at once

Hi there. On the main webpage for this script, I can press up to 6 keys on my keyboard and see them light up on your example keyboard. However, when I implement the code on my site, if I am holding down two keys and then press a third on, the "on_keydown" function does not get called. Is there an obvious reason for this? How do you accomplish detecting SIX keypresses at once?

Function keys (F1-F12)

Could we get support added for the function keys? I know that they're often used for browser actions that can't be blocked, but not all of them are, and in some cases they're the keys that make the most sense.

One possible issue with binding to function key presses (and a corresponding solution) is presented at http://www.sidesofmarch.com/index.php/archive/2007/06/28/capturing-function-keys-in-web-browsers/.

I see that issue #11 started out being about support for the function keys, but seems to have also included another issue that got resolved. (with the addition of is_solitary)

Sequence Timing

I've created a sequence that when I hit " " (tick tick) I want it to trigger an action.

I can get it to trigger, and it pops up an alert as expected, but then the very next tick I type into say a text field, its popping up the alert again.

My question is, is there any time threshold or setting for that per sequence so that I can say if I typed two ticks in a row, within 500ms, treat it as a sequence, otherwise just let the characters through?

The other thing to note, even though it was triggering the desired action, it was not actually blocking the keypresses in those events (ticks ended up in the textbox)

Right Alt triggers Left Ctrl in Windows on Polish keyboard

We received reports on the mis-triggered events of Right Alt key when using some non-US keyboard, for exmaple, Germen, Polish. After we look into the details, we found that Right Alt key also tirggers Left Ctrl key, whose keyCode is 17 with keyIdentifier of Control.

The bug can be reproduced on Windows 7 Chrome, Polish programmer keyboard, pressing Right Alt.

There's related issue on StackOverflow: http://stackoverflow.com/questions/7090898/right-alt-key-also-triggers-windows-message-for-left-control-key

It's probably a bug of Windows. But in Keypress people may also encounter this problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.