tttt
tttt

Reputation: 49

keyboard switch button shortcut with js

I want to create a keyboard shortcut that goes to one function, and when you press that key again, it returns to the original function.

For example I am using a paint tool, but I want to switch to an eraser by pressing something like tab, then when I'm done I switch back to the paint tool by pressing tab again.

I've done keyboard shortcuts before, but I've never done one that returns to the previous function...

Sorry if this is not clear, I'm not sure where to begin. Even if someone could guide me to a link that I can study I would really appreciate it.

Upvotes: 0

Views: 960

Answers (2)

Aykut Ateş
Aykut Ateş

Reputation: 184

Try this

var currentTool = 'brush';
var prevTool = '';

$('.paintToggle').keypress(function (e) { 
  var key = e.which; 
  switch (key) {
    case 6:
      if(currentTool !== 'eraser') {
        prevTool = currentTool;
        currentTool = 'eraser'; 
      } else {
        currentTool = prevTool;
      }
      break;
  }
  paint.select_tool(currentTool);
});

Upvotes: 1

Mahbub Alam
Mahbub Alam

Reputation: 368

Use the below snipset. Here, you should introduce yourClass name. Then action should be invoked for your key pressing.

$('.yourClass').keypress(function (e) {
        var key = e.which;
        if(key == 13)  // the enter key code
        {
           //to do your action
        }
});

Upvotes: 0

Related Questions