2

How can I call a javascript function on CTRL + Space?

function getdata() {
    console.log("hello");
}

When I hit the getdata() function on CTRL + Space and gives me autosuggestion.

  • If user type something on my textbox like sta
  • User types CTRL + Space
  • It should give me a auto suggestions like stack, stackover, stackoverflow
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
SAGAR MANE
  • 685
  • 2
  • 8
  • 23

3 Answers3

1

In order to accept the keyboard input from CTRL + SPACE you'll need to register an event handler (http://www.quirksmode.org/js/events_tradmod.html) and then listen out for input from the user. This will read from an array of keystroke events and check whether they're true, then fire event when keys have been pressed, when they get released the events are false.

var map = {17: false, 32: false};
$(document).keydown(function(e) {
    if (e.keyCode in map) {
        map[e.keyCode] = true;
        if (map[17] && map[32]) {
            // FIRE EVENT
        }
    }
}).keyup(function(e) {
    if (e.keyCode in map) {
        map[e.keyCode] = false;
    }
});

if you go to this link: cambiaresearch.com/articles/15/javascript-char-codes-key-codes, you'll find that the keycodes are all listed here. 17 and 32 is CTRL + SPACE.

check out this guide on auto_complete with JQuery. This code will be executed where the event is fired.

https://github.com/mliebelt/jquery-autocomplete-inner

0

Look at this answer. - https://stackoverflow.com/a/16006607/2277126

Here you have list of key codes key codes

Good luck!

Community
  • 1
  • 1
OrMoush
  • 195
  • 1
  • 7
0

Here's how I got JQuery autocomplete to show its dropdown list when the user presses Ctrl + space:

$( "#" + myElementId )
  .on( "keydown", function( event ) {
    // Ctrl+space opens the autocomplete dropdown
    if (event.keyCode === $.ui.keyCode.SPACE && event.ctrlKey ) {
      $(this).autocomplete("search");
    }
  });
Pi Da
  • 359
  • 2
  • 8