Detect if the ctrl button was pressed - javascript

I want to prevent users from printing a page
I thought I would set the screen to not include the toolbars, and prevent right clicks, and prevent Ctrl+ P, and the Print Screen button.
Can this be done?
Is there any good code out there for this? I have searched quite a bit so far, but not much luck. I know this isn't foolproof, but it will prevent some users from copying or printing.

You can't do this...you can't disable the user's ability to print, nor should you try.
Ctrl+P is the way a programmer prints, File > Print (depending on browser) is the way the typical user does...so this wouldn't even disable the most common method. In addition, any decent programmer can get around this anyway, so it effectively doesn't stop anyone.
Any data you get to a user, displayed or not, they can see, copy, print, etc...there's nothing you can do to prevent this, definitely not 100%. If this is one of your requirements...you should be asking if a website is the best way to deliver this data.

By doing that, you will annoy legitimate users, and if you think a serious copyright violator uses a regular browser (whose printing function you can disable), then you're very mistaken.

On the one hand information wants to be expensive, because it's so valuable. The right information in the right place just changes your life. On the other hand, information wants to be free, because the cost of getting it out is getting lower and lower all the time. So you have these two fighting against each other. source
Also:
Information Wants To Be Free, and Code Wants To Be Wrong.

I agree with the answers above.
Users will always find a way around this,
- A computer is not secure for copyrighted material and will never be.
you need to take that into account.

If you'd want to make it so that regular computer users can't do it this would help:
Create an application that loads and displays the document after input of a keycode that you supply (check via webserver).
the application does not have printing functions since you did not put them in
register a global keyhook to blank the document if the user presses "printscreen" and show a copyright warning

A couple of years ago, I built an exam system where one of the requirements was to make it hard for people to print the exams. Removing the print functionality is as we know, impossible (unless you do some changes in the browser software). What you can do is to make it harder for non-technical people to print the page. E.g. Use CSS to blank the page when it goes to the printer:
<style type="text/css">
#media print {
body { display:none }
}
</style>
The following jQuery script will prevent copy&paste in some browsers:
$(document).ready(function () {
$(document).bind('copy paste', function (e) {
e.preventDefault();
});
});

the #Nick Craver answer is right, you can't prevent it but anyways if you want to detect the key combination using mootools you have the keyboard class that let you define key combinations and add events to it:
http://mootools.net/docs/more/Interface/Keyboard
that maybe will be useful to display a warning or something like that :)

You could so with jQuery for example. However think of this: a browser runs on a client pc which is owned by someone. That person should be in control of what happens on his/her device. It's not up to you to start putting scripts to get rid of standard functionality the enduser might want to use.
If you don't want something to be printed then don't show it on a public place. If it's confidential, treat it as such.
Grz, Kris.

Take this into account. I agree with the other answers and present another way around this. All the user must do is take a screenshot, which involves the application layer of the operating system, and one of which you cannot even hope to change. On Ubuntu, it's even in the user's main menu to do this.

<script type="text/javascript">
function detectspecialkeys(e){
var evtobj=window.event? event : e
if (evtobj.altKey)
alert("you pressed 'Ctrl'");
evtobj.preventDefault();
}
document.onkeypress=detectspecialkeys
</script>

Related

Is there a way to stop users cutting and pasting content from my HTML pages? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have content that has taken me a long time to create. From what I understand there is no way to stop a person getting at the content and copying it if they access the source. However is there a way to stop the users from doing a simple copy from the screen? A non jQuery way. Thanks
Rules of the web 101: if it is on their screen, they can have it. That's because in order for the user to see content on your site, your site must first enter the realms of their territory, their turf, their house. In order for people to see your website, the stuff that is on your website must travel along the power lines that eventually leads in to their home. Once that information is in their home, it's just a matter of keeping it there. It's just the way emissions work.
You can use tricks to make it harder such as converting your text to an image so the user has to type it out again, but OCR can work around that, and it'll take up more precious space on your site. You can disable right click, but this is easy to work around. Not only do browser extensions exist to re-enable right clicking, Javascript could easily be turned off in any browser, breaking that protection. You could use Flash to protect your text, pretty much the same case scenario as an image. No matter what you do, the user can in some way record or recreate your content. If it is copyrighted, the only way is to use the legal system to ensure there are no unauthorized copies, but once again, the rules of the web state if it is on their screen, they can have it.
Some answers will probably suggest these tricks to make it harder, and you can implement them, but they are never foolproof and they might scare away what was otherwise a potential fan or customer. Since I know all this, if I see a site that disables right click I feel like the site owner doesn't know how useless it is what they're doing, and by extension don't know what they're doing at all.
anybody who even know how to access the developer tools of the browser will tear down the page and get the content needed anyway...so if you ask me it is not really worth the effort and plus it just gets annoying to users and a bad UI design .
but even if you disregard the above paragraph then here is a link you can refer.. http://www.hypergurl.com/norightclick.html
here is another way you can slow down people from copying images..
<script type="text/javascript">
//<![CDATA[
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Disable context menu on images by GreenLava (BloggerSentral.com)
Version 1.0
You are free to copy and share this code but please do not remove this credit notice.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
function nocontext(e) {
var clickedTag = (e==null) ? event.srcElement.tagName : e.target.tagName;
if (clickedTag == "IMG") {
alert(alertMsg);
return false;
}
}
var alertMsg = "Image context menu is disabled";
document.oncontextmenu = nocontext;
//]]>
</script>
Now this diables right click on images that was defined with "img" tag....you can extend this to other tags as well.
But for usability sake dont do it as it is not worth it and also wont achieve anything.
DO NOT DO THIS
...As if you are gonna listen to me. One of the most straightforward ways is to use the built-in event copy. The event is trigerred when someone copies anything from the element to which the event is attached.
document.getElementById('myContent').oncopy = function() {
/*alert("do something if you want to here")*/
return false;
};
input {
width: 400px;
}
<span id="myContent">Your content which you don't wanna be copied!</span>
<br/>
<input placeholder="try copying and pasting the above here to test" />
See the above for a simple test. You can attach the event to the html element to add the effect to all the elements.
DO NOT DO THIS
P.S. Check browser support for the copy event.

JavaScript, Check if website is rendered and displayed on the screen

I was wondering if there are any methods to check if a website is successfully displayed or rendered on the a user's system.
The application of this is to to deliver a content if and only if it is a real user rather than a crawler/spider fetching the content.
so the check would be:
-check if the content is rendered/displayed,
-execute the next script
-otherwise
-do something else
any help is highly appreciated.
Cheers
Most crawlers simply do not execute any JavaScript, but you cannot really rely on that since it's easy to imagine a sophisticated company creating a search engine that actually does mimic a JS-enabled browser. Many crawlers have an easily identifiable user-agent string, but you cannot rely on that either.
You could do something, I suppose, like attempt to poll for the mouse x,y position a couple of times, looking for values other than 0,0, which is likely to indicate a person with an actual computer and pointing device is at the other end. That still may not get what you want for touch screen devices though. You might also consider waiting until you detect a scrolling event if your secondary scripts don't need to load right away.

Catch back-button (keep one history entry, no visible hash-tags)

We are currently building an advanced JS/AJAX-based service, and allowing the back-button to function in a meaningful way is obviously important. I've looked around some, and been in-contact with iframe-hash/anchor-solutions before, but the ones I've seen and found are all based on the idea of going back/forwards in a page structure. What I'm looking for is to find a way to bind the back-button to act as an alternative for "close" / "cancel".
The solution to what I'm looking for is simple, to almost always keep exactly one additional back-history entry (except for when "we" decide the user is at the "start"), without showing a hash-tag in the location bar.
I did have a try at implementing it myself, a hidden iframe for which I change the hash, worked well, in all browsers but IE, it refused to update the hash-tag for me when pressing back. Are there any solutions out there that solve this problem (trap back-button, no visible hash-tag)? Or any simple cross-browser solutions for dealing with hidden iframe hashtags?
Sorry for the partial answer, but this looks promising: http://code.google.com/p/reallysimplehistory/
I'd like to keep informed of your progress on this, it's a useful thing if it's practical to implement.
Dan

Listen to HTML/CSS events of web page

I'm looking for a way to listen for an event in the HTML/CSS of a webpage. Basically, a website I use at work lists queues and appointments, which flag as yellow/red based on the amount of time late on taking said appointment. Other than this visual aide, there is no alert of any kind, which is a failing point if one is juggling multiple windows and doesn't notice.
What I would like to do is put together a simple way to watch for the tag ID or perhaps the flag hex color, and then alert me in a way of my choosing.
The work environment is on OS X, so I presume I'm looking at an Objective-C centric solution unless Applescript is capable of doing this.
Any help would be greatly appreciated.
I know you didn't tag jQuery, or JavaScript, but a custom bookmarklet may be the solution.
As it so happens I created a bookmarklet utility a while back so that you can keep your bookmarklet in an external script file.
You could then have alert or confirm called automatically for some event on the page.
Are you able to specify the browser your users use? If so, Chrome has support for HTML5's out of browser notifications, ala: http://slides.html5rocks.com/#notifications-api
You could get a reference to the dom elements you want to monitor, and use setInterval() to poll the data you are looking for on some interval, alerting when it changes.
See this fiddle for an example.

Need a cool hotkey for my hidden website-login

I am developing a CMS. One of the greatest everyday annoyances when working with it is that when you are on a front-end page, not logged in, and want to make a change, you need to go to the back-end login first, log in, and then navigate to the page you want to change.
I would like to introduce a smart little function that pops up a log-in screen when you hit a certain key combination. I have already set this up with double Ctrl. I have come to realize, though, that I hit that key so frequently that the login pops up unwanted, something that must not happen of course.
So, I am asking for your ideas for a clever key combination.
I am looking for a key combination that is:
Elegant and easy to memorize (this is why I liked double Ctrl very much)
Time considerations (Press key X, then Y after 1 second) are possible
Must not be mapped to any browser function of course. I realize this cannot be excluded 100% but obviously, Ctrl+A or Alt+Tab would be nonstarters.
Works on all keyboards (Mac included, but I'm willing to handle the apple special keys)
Extremely unlikely to be hit accidentally
Other clever ideas like simple mouse gestures are also welcome.
It can be argued that this poses a decrease in security, as the URL to the back-end login is made available in front-end pages and loses the additional protection by being obscure. I may address that later using a cookie that identifies CMS users' machines but it is not the issue right now.
Oh you just KNOW you have to go for the Konami Code!
There are several websites that implement it - take a look at this list.
I know it's not the simplest, but it does have the geek factor ;)
Why not just have a "Editor Login" link at the bottom of the page?
It won't be any less secure and will be a lot easier to use. I can see the solution you are trying to use causing all kinds of odd problems down the road...
If you really really want it "hidden", you can just make it white text on a white background or something.
What about the simple ` key at the top left corner of your keyboard? This is often used in games to get into a console mode (counter strike, left 4 dead, etc). It is mac/windows/linux compatible and shouldn't interfere with anything in the browser.

Categories