Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine key codes to work together in javascript

E.g. I have the following script

<script>
document.onkeydown = function(evt){
        evt = evt || window.event;
        switch (evt.keyCode){
            case 67:
                createNewFile();
                break;
            case 82:
                goToRecords();
                break;
            case 84:
                goToToday();
                break;
            case 36:
                goToMsHome();
                break;
            case 27:
               escToCloseOptions();
                break;
            case 83:
               summary();
                break;  
            case 73:
               insertRecord();
                break;          
        }
    };
</script>

When I press

shift + keycode

to call a function specified, I am just new to JavaScript and I code JavaScript based on other languages I know

Thanks and Regards

like image 733
user2985621 Avatar asked Oct 21 '22 19:10

user2985621


1 Answers

Check .shiftKey which returns a boolean indicating if the key is pressed. Placing this conditional around your switch will prevent any events from occurring unless the key is pressed in combination with shift.

document.onkeydown = function(evt){
        evt = evt || window.event;
        if(evt.shiftKey){
        switch (evt.keyCode){
            case 67:
                createNewFile();
                break;
            case 82:
                goToRecords();
                break;
            case 84:
                goToToday();
                break;
            case 36:
                goToMsHome();
                break;
            case 27:
               escToCloseOptions();
                break;
            case 83:
               summary();
                break;  
            case 73:
               insertRecord();
                break;          
        }
      }
    };
like image 192
Kevin Bowersox Avatar answered Oct 27 '22 09:10

Kevin Bowersox