0
  1. With the code below, is there anyway of preventing the user from entering special characters that are generated by pressing CTRL + ALT + 4 for example?
  2. That produces the euro currency sign. All the below code works perfectly, I just need to prevent any special characters that are generated from CTRL + ALT
  3. Prevent the user from using their mouse to right click and paste the content in
  4. Working with IE8

`

$("#txtboxToFilter").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
         // Allow: Ctrl+A
        (e.keyCode == 65 && e.ctrlKey === true) ||
         // Allow: Ctrl+C
        (e.keyCode == 67 && e.ctrlKey === true) ||
         // Allow: Ctrl+X
        (e.keyCode == 88 && e.ctrlKey === true) ||
         // Allow: home, end, left, right
        (e.keyCode >= 35 && e.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
    }
    // Ensure that it is a number and stop the keypress
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});`
MegaTron
  • 3,133
  • 6
  • 28
  • 45
  • There's also `e.altKey` property. – Teemu Jun 15 '15 at 11:17
  • check [http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once](http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once) – Amit.rk3 Jun 15 '15 at 11:18
  • Teemu, I think that worked by doing the following: if ((e.shiftKey || e.altKey – MegaTron Jun 15 '15 at 11:22
  • Is there a way of stopping the user from using their mouse to paste the character in? – MegaTron Jun 15 '15 at 11:27
  • 1
    Well, there is – but – it's useless, there are many other ways to add a character, from clipboard or another way, you simply can't block all of them. Looking your code, it seems that the simplest way to do what you actually need, is to check the value with RegExp when oninput fires, and that way filter out the unwanted characters. – Teemu Jun 15 '15 at 12:37

1 Answers1

0

If you want to allow only certain keyboard shorcuts, you can disable anything but whatever you let pass. You can also decide to list everything that you want disabled, and allow everything else to execute. A simple way I see this can go is to disable the Ctrl key and the Alt key if they are pressed simultaneously, as such:

$("#txtboxToFilter").keydown(function (e) {
    if (e.ctrlKey === true && e.altKey === true) {
         return;
    }
    else {
        //Do whatever;
        if (e.key === "Tab") {
            //Tab was pressed
        }
        else if (e.key === "H") {
           //Shift H was pressed
        }
        else if (["Home", "End", "PageUp", "PageDown"].includes(e.key) === true) {
            //Navigational Keys Were Pressed
        }
        else if (["ArrowUp", "ArrowDown", "ArrowRight", "ArrowLeft"].includes(e.key) === true) {
            //Directional Arrows Were Pressed
        }
    }
 });

And may I recommend that you use e.key instead of e.keyCode, e.which, or code, because it is more supported, and it is way easier to understand. Just take a look at the code snippet above, there are examples of e.key. Besides, there is no confusion with numbers, because the key names are used. If you wanted to use the Windows Key on Windows, the Search key on Chromebooks, e.key === "Meta" is the way to go.

Hope this extra information helps!!!

Da Mahdi03
  • 1,468
  • 1
  • 9
  • 18