Wael Sayed

Wael Sayed

ODESK

The On Demand Global Workforce - oDesk

Thursday, June 18, 2009

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 any application want to
    protect.

    Some people can hack your data by 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



First step is to avoid using print screen key To solve this
problem you have to access the user’s clipboard by java script 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 set a new clipboard in a message
as previous example.



If the user press print screen key he or she can not capture any image because
the clipboard contains a text message says "No print data"


So don't forget to close this page if you 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 IE will ask user if allow this page to
access the clipboard or not so we have to write code to handle this option if
the user not allow the page to access the clipboard



Add this code after 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"
}


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



Second step is avoid using keyboard


To solve this problem you have to capture all events and cancel it by pop
message 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;
}


Third step is 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 appear in HTML source so we will use update panel
and if your data get in page_load event delay showing data by timer control