6

I want to disable any image selecting in my website. I already use this code:

  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;

but still have a problem with CTRL+A. Is there any jQuery or Javascript code to disable CTRL+A and select all function?

EDIT : this is my website > http://narenjco.ir/ press ctrl+a in first page , i want to make sparks unselectable

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Mori uzx
  • 89
  • 1
  • 7

3 Answers3

8

Edit: I now see that you've applied it to the images within the .unselectable class. In Chrome these images are unselectable, so the issue appears to be browser-specific. To overcome this problem in whichever browser you're using you'll need to use JavaScript.


It's all to do with the usage of your user-select:none. Currently, I can't see on your site where this has been applied, however applying it to the body tag does prevent Ctrl+A from selecting any content (in Chrome at least).

body {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
}
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
4

Try this

$(function(){   
    $(document).keydown(function(objEvent) {        
        if (objEvent.ctrlKey) {          
            if (objEvent.keyCode == 65) {                         

                return false;
            }            
        }        
    });
});   
Sudz
  • 4,268
  • 2
  • 17
  • 26
  • 2
    You've copied the answer from a duplicate question: http://stackoverflow.com/questions/2403748/how-can-i-disable-ctrla-select-all-using-jquery-in-a-browser?lq=1 – MMM May 15 '13 at 09:57
  • 1
    May be, but it will work – Sudz May 15 '13 at 09:59
  • It's either you copied the answer from Marcx or you both copied the code from a website which I doubt Marcx would do. – Michael 'Maik' Ardan May 15 '13 at 09:59
  • 4
    It's ok to copy code from another answer, but if you do so you should credit the original author. – JJJ May 15 '13 at 10:03
  • Use `event.key` instead. `event.keyCode` is `deprecated`.https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key – Iglesias Leonardo Apr 20 '22 at 14:49
0

You can listen to key press events and return false on Ctrl+A

$(document).keypress(function(e) {
    return e.keyCode != 65 || e.ctrlKey == false; // Return false only on Ctrl+A
}
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
dtech
  • 13,741
  • 11
  • 48
  • 73