How to Intercept All Key Events, Including Ctrl+Alt+Del and Ctrl+Tab

How can I intercept all key events, including ctrl+alt+del and ctrl+tab?

To add to what Shog9 said, if your application could intercept ctrl+alt+del, then your application would be able to pretend to be the Windows Login dialog, and by doing so trick the end-user into typing their credentials into your application.

If you do want to replace the Windows Login dialog, see Winlogon and GINA (but this says, "GINA DLLs are ignored in Windows Vista", and I haven't heard what's what for Vista).

if someone asked I'd not tell them they can't.

More specifically, your "application software" can't: instead, by design, only "system software" can do this; and it isn't that you're not allowed to or not able to write system software, but your OP seemed to be quite clearly asking how to do it without writing system software ... and the answer to that is that you can't: because the system is designed to prevent an application from hooking these key combinations.

Can you give me direction to writing the system things.. I actually think this would be better if it were system level.. It's for an OEM so kind of the point really. Also if I wrote it system level, I could write an app to control it.

A keyboard filter device driver, or a GINA DLL, for example, would be considered system software: installed by an administrator (or OEM) and run as part of the O/S.

I don't know about GINA beyond its name; and I've already (above) given a link it in MSDN. I expect that it's Win32 user-mode code.

Device drivers are a different topic: e.g. Getting Started on Driver Development.

Is there a way to remap the keyboard so that delete isn't where it was?

I still not sure that you and/or your boss have the right idea. IMHO you shouldn't be an application which prevents the user from pressing Ctrl-Alt-Del. If you want to stop the user from accessing the system without typing a password, then you ought to lock (password-protect) the system, as if the user had pressed Ctrl Alt Del and then selected "Lock this computer". To unlock the computer they would then need to press Ctrl Alt Del and enter their credentials into WinLogon.

However, ignoring what you ought to do and concentrating instead on what you're capable of doing, if you want to intercept the keyboard, apparently it can be done. I haven't studied keyboards myself, but this post and this post claim success, by writing a "Keyboard Filter Driver" (which is a kind of kernel-mode, not Win32, device driver). If you write one of these though you may get some push-back, e.g. like this reaction from a DDK MVP, or this reaction from an anti-snooping product.

Capturing Ctrl-Alt-Del in JavaScript/jQuery

I'm not sure about other operating systems, but I know that on Windows, Ctrl+Alt+Del is a system keystroke, which is intercepted at the top level, always by Windows. As far as I know, it is not passed any further on from there, allowing you to KNOW that whatever comes up from it is official.

I know that for Gnome (and I believe KDE), hitting it pulls up the shutdown dialog, and thus, is captured by the window manager, and not passed on.

So, unless Mac just ignores it, no, you cannot get that particular combination, due to its magic to the OS.

EDIT: just something I noticed, you have alter not alert in your code. I don't think it would work either way with it like that.

Detecting combination keypresses (Control, Alt, Shift)?

Refer to the W3C spec for keyboard events. Several boolean attributes are provided to determine if modifier keys were pressed in conjunction with whatever target key you are interested in. They are:

  • ctrlKey     -- The "Control" key was also pressed.
  • shiftKey   -- The "Shift" key was also pressed.
  • altKey       -- The "Alt" key was also pressed.
  • metaKey     -- The "Meta" key was also pressed.

Other important notes:

  1. The which property is deprecated.
  2. Use keydown because Chrome does not fire the keypress event for known keyboard shortcuts.
  3. Some spec'd properties, such as key, are only partly functional in Firefox.
  4. You do not need to wrap your code in an anonymous function like that for Tampermonkey (or Greasemonkey or most userscript engines). Scope protection is automatically provided.

So, your code would become:

document.addEventListener ("keydown", function (zEvent) {
if (zEvent.ctrlKey && zEvent.altKey && zEvent.key === "e") { // case sensitive
// DO YOUR STUFF HERE
}
} );

Run this handy demo (updated now that key has full support):

var targArea = document.getElementById ("keyPrssInp");targArea.addEventListener ('keydown',  reportKeyEvent);
function reportKeyEvent (zEvent) { var keyStr = ["Control", "Shift", "Alt", "Meta"].includes(zEvent.key) ? "" : zEvent.key + " "; var reportStr = "The " + ( zEvent.ctrlKey ? "Control " : "" ) + ( zEvent.shiftKey ? "Shift " : "" ) + ( zEvent.altKey ? "Alt " : "" ) + ( zEvent.metaKey ? "Meta " : "" ) + keyStr + "key was pressed." ; $("#statusReport").text (reportStr);
//--- Was a Ctrl-Alt-E combo pressed? if (zEvent.ctrlKey && zEvent.altKey && zEvent.key === "e") { // case sensitive this.hitCnt = ( this.hitCnt || 0 ) + 1; $("#statusReport").after ( '<p>Bingo! cnt: ' + this.hitCnt + '</p>' ); } zEvent.stopPropagation (); zEvent.preventDefault ()}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><p><label>Press keys in here:<input type="text" value="" id="keyPrssInp"></label></p><p id="statusReport"></p>

Check Ctrl / Shift / Alt keys on 'click' event

Well you this wont work in all browsers just IE 8. Microsoft implemented the ability to determine which (right/left) key was pressed. Here is a link http://msdn.microsoft.com/en-us/library/ms534630(VS.85).aspx

I also found this wonder article about keypress, keyup, keydown event in browsers.
http://unixpapa.com/js/key.html

$('#someelement').bind('click', function(event){ 

if(event.ctrlKey) {
if (event.ctrlLeft) {
console.log('ctrl-left');
}
else {
console.log('ctrl-right');
}
}
if(event.altKey) {
if (event.altLeft) {
console.log('alt-left');
}
else {
console.log('alt-right');
}
}
if(event.shiftKey) {
if (event.shiftLeft) {
console.log('shift-left');
}
else
{
console.log('shift-right');
}
}
});


Related Topics



Leave a reply



Submit