Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable key #431

Closed
traumatic opened this issue Feb 4, 2016 · 4 comments
Closed

Disable key #431

traumatic opened this issue Feb 4, 2016 · 4 comments

Comments

@traumatic
Copy link

Hi, I was wondering whether Keyboard provides a function which allows to disable keys.
E.g. in my case I want that the space key is disabled as long as text input is empty.

Thanks!

@Mottie
Copy link
Owner

Mottie commented Feb 4, 2016

Hi @traumatic!

If you look at the various demos available on the Home wiki page, you'll find this one - "Disable Accept & Bksp keys when preview is empty".

I took that demo and modified it to include the space: http://jsfiddle.net/Mottie/egb3a1sk/1161/

$(function() {

  var toggleKeysIfEmpty = function( kb ) {
    var toggle = kb.$preview.val() === '';
    kb.$keyboard
      .find('.ui-keyboard-bksp, .ui-keyboard-accept, .ui-keyboard-space')
      .toggleClass('disabled', toggle)
      .prop('disabled', toggle);
  };

  $('#keyboard').keyboard({
    visible : function(e, keyboard) {
      toggleKeysIfEmpty( keyboard );
    },
    change: function (e, keyboard) {
      toggleKeysIfEmpty( keyboard );
    }
  });

});

Is that what you wanted?

@traumatic
Copy link
Author

Thanks Mottie. That example works very well.

I replaced
var toggle = kb.$preview.val() === ''; with
var toggle = kb.$preview.val() === '' || kb.$preview.val().indexOf(" ") > -1;

Now type any first letter and afterwards space. In my case that leads to multiple spaces being added.

@Mottie
Copy link
Owner

Mottie commented Feb 4, 2016

LOL, that is interesting....

When the space key is used, the toggleKeysIfEmpty function disables the space and thus prevents a "mouseup" event from firing. So, since there is no "mouseup" the keyboard starts firing a "repeater" event because it is set up to repeat the last key if it is held down.

I tried to clear the repeater in that function, but it still adds an extra space. So the easiest solution would be to set the repeatRate option to 0 - demo.

I'll push a fix for this - that doesn't need repeatRate to be set to 0 - to the master branch in a minute.

@traumatic
Copy link
Author

Wow, thanks for the quick fix Mottie. Keep up that great work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants