Click here to Skip to main content
11,634,631 members (68,177 online)
Click here to Skip to main content

Disable Print Screen Key and All Keyboard Keys in ASP.NET Page

, 18 Jun 2009 CPOL 83.5K 3.3K 23
Rate this:
Please Sign up or sign in to vote.
Disable print screen key and all keyboard keys in ASP.NET page

Introduction

In this article, I will explain how to disable print screen or keyboard in secure applications as Exam online in E-learning system or in any application you want to protect.
Some people can hack your data by using print screen, take HTML source code or even print the questions.

We have 4 challenges to create pages for exams or secure application:

  1. Disable print screen and clear any Clipboard data
  2. Avoid using keyboard or mouse
  3. Hide data in HTML source 
  4. Avoid using print page

The first step is to avoid using print screen key

To solve this problem you have to access the user’s clipboard by JavaScript function:

<script language="javascript" type="text/javascript">
    function AccessClipboardData() {
        try {
    window.clipboardData.setData('text', "No print data");
        } catch (err) {
   txt = "There was an error on this page.\n\n";
   txt += "Error description: " + err.description + "\n\n";
   txt += "Click OK to continue.\n\n";
    alert(txt);
        }
    }
</script>

This function clears any clipboard object and sets a new clipboard in a message as the previous example.

If the user presses print screen key, he or she cannot capture any image because the clipboard contains a text message that says "No print data". So don't forget to close this page if you would like to copy a text or a file in your operating system.

We call this function(AccessClipboardData) every 300 milliseconds by using setInterval to clear any object in clipboard.

 setInterval("AccessClipboardData()", 300);

But this solution not complete because Internet Explorer will ask the user if he/she will allow this page to access the clipboard or not. So we have to write code to handle this option if the user will not allow the page to access the clipboard.

Add this code after the setInterval function:

setInterval("AccessClipboardData()", 300);
var ClipBoardText = "";
         if (window.clipboardData) {
             ClipBoardText = window.clipboardData.getData('text');
             if (ClipBoardText != "No print data") {
               alert('Sorry you have to allow the page to access clipboard');
// hide the div which contains your data          
document.all("divmaster").style.display = "none"
             }

In the previous code, we declare a clipboardtext variable to get clipboard text and check if the user allows the page to access the clipboard or not.

The second step is to avoid using keyboard

To solve this problem, you have to capture all events and cancel them by pop messages in case of copying data by CTRL+C or select all data by CTRL+A.

document.onkeydown = function(ev) {
  var a;
   ev = window.event;
   if (typeof ev == "undefined") {
     alert("PLEASE DON'T USE KEYBORD");
          }
       a = ev.keyCode;
        alert("PLEASE DON'T USE KEYBORD");
              return false;
         }
       document.onkeyup = function(ev) {
           var charCode;
        if (typeof ev == "undefined") {
         ev = window.event;
         alert("PLEASE DON'T USE KEYBORD");
           } else {
          alert("PLEASE DON'T USE KEYBORD");
           }
         return false;
       }

The third step is to avoid using print page

To solve this problem, you have to use CSS (Cascade Style Sheet) to hide your data in printing:

<style type="text/css" media="print">
.noprint {
display: none;
}
</style>

Finally we want to avoid data from appearing in the HTML source, so we will use update panel and if your data gets in page_load event delay shows data by timer control.

History

  • 18th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wael Sayed
CEO ProNileSoft
Egypt Egypt
I have 8+ years experience in Web development ASP.NET, XML,HTML,CSS,Ajax and JavaScript.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
karma.laabhaa24-Jun-14 0:09
memberkarma.laabhaa24-Jun-14 0:09 
GeneralThanks Pin
Varun Verma191010-Aug-12 2:25
memberVarun Verma191010-Aug-12 2:25 
GeneralRe: Thanks Pin
manavrohit26-Feb-13 22:45
membermanavrohit26-Feb-13 22:45 
BugPrint Screen Still Functioning Pin
Member 20618254-Mar-12 16:08
memberMember 20618254-Mar-12 16:08 
GeneralHow you disabled HTML view source Pin
Sankaroo719-May-11 16:26
memberSankaroo719-May-11 16:26 
Generalalternative lock key Pin
Tybalt3326-Nov-09 1:00
memberTybalt3326-Nov-09 1:00 
Questionhow to combined this code with the code i wrote? Pin
yellow6619-Nov-09 19:48
memberyellow6619-Nov-09 19:48 
QuestionWhat if I want to disable on print screen Pin
Aman Bhullar21-Aug-09 19:45
memberAman Bhullar21-Aug-09 19:45 
GeneralRe: IE 8 Popup Pin
sevensnake7727-Jul-09 5:39
membersevensnake7727-Jul-09 5:39 
GeneralMy vote of 1 Pin
James Luterek22-Jun-09 3:07
memberJames Luterek22-Jun-09 3:07 
GeneralRe: My vote of 1 Pin
Wael Sayed23-Jun-09 7:57
memberWael Sayed23-Jun-09 7:57 
GeneralUse This Pin
aa.azizkhani19-Jun-09 23:48
memberaa.azizkhani19-Jun-09 23:48 
GeneralExcellent Pin
Alex_Jones19-Jun-09 7:17
memberAlex_Jones19-Jun-09 7:17 
GeneralMy vote of 1 Pin
taxexile18-Jun-09 11:34
membertaxexile18-Jun-09 11:34 
GeneralGood Pin
James_200918-Jun-09 11:16
memberJames_200918-Jun-09 11:16 
GeneralWaste of time... Pin
Axel Rietschin18-Jun-09 10:22
memberAxel Rietschin18-Jun-09 10:22 
GeneralRe: Waste of time... Pin
Mi.Chal.19-Jun-09 23:15
memberMi.Chal.19-Jun-09 23:15 
GeneralRe: Waste of time... Pin
Tatworth16-Feb-10 6:47
memberTatworth16-Feb-10 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 18 Jun 2009
Article Copyright 2009 by Wael Sayed
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid