How to grab keyboard events on an element which doesn’t accept focus?

A div by default cannot be given focus. However, you can change that by adding a tabindex attribute to the div: <div tabindex=”0″ id=”example”></div> You can then give the div focus, and also blur it with the hover event: $(“#example”).hover(function() { this.focus(); }, function() { this.blur(); }).keydown(function(e) { alert(e.keyCode); }); When the div has focus, … Read more

change keyboard layout with javascript

You won’t be able to change the keyboard layout using JS, but you can capture the keydown event and replace the character with something like this: http://jsfiddle.net/SxdKZ/ $(‘textarea’).on(‘keydown’, function(e){ console.log(e.keyCode); if( e.keyCode == 90 ){ e.preventDefault(); $(this).append(‘y’).focus(); } if( e.keyCode == 89 ){ e.preventDefault(); $(this).append(‘z’).focus(); } });​

How to get a combination of keys in c#

It is a chord, you cannot detect it without memorizing having seen the first keystroke of the chord. This works: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool prefixSeen; protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (prefixSeen) { if (keyData == (Keys.Alt | Keys.Control | Keys.P)) … Read more

Handling key-press events (F1-F12) using JavaScript and jQuery, cross-browser

I agree with William that in general it is a bad idea to hijack the function keys. That said, I found the shortcut library that adds this functionality, as well as other keyboard shortcuts and combination, in a very slick way. Single keystroke: shortcut.add(“F1”, function() { alert(“F1 pressed”); }); Combination of keystrokes: shortcut.add(“Ctrl+Shift+A”, function() { … Read more

How to handle key press event in console application

For console application you can do this, the do while loop runs untill you press x public class Program { public static void Main() { ConsoleKeyInfo keyinfo; do { keyinfo = Console.ReadKey(); Console.WriteLine(keyinfo.Key + ” was pressed”); } while (keyinfo.Key != ConsoleKey.X); } } This will only work if your console application has focus. If … Read more

tech