keydown

keydown + keyup events for specific keys

老子叫甜甜 提交于 2019-11-30 03:54:03
I'm trying to make the background color change when certain keys are held down. For example, when the 'r' key is being held down, the background should be red. When the 'r' key is not being pressed anymore, the background should default to white. $(document).ready(function () { $('body').keydown(function(e){ if(e.keyCode == 114){ $(this).css({'background':'red'}); } if(e.keyCode == 121){ $(this).css({'background':'yellow'}); } }); $('body').keypress(function(e){ if(e.keyCode == 114){ $(this).css({'background':'red'}); } if(e.keyCode == 121){ $(this).css({'background':'yellow'}); } }); $('body'

jQuery - keydown() on div not working in Firefox

守給你的承諾、 提交于 2019-11-30 01:19:05
I have the following example code, which should pop up an alert when the div is in focus and a key is pressed. This does what I expect in IE 7, but not in Firefox 3.5.5. What am I doing wrong? <html> <head> <title>JS test</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#testdiv").keydown(function(event) { alert("Pressed " + event.keyCode); }); }); </script> <style type="text/css"> #testdiv { width: 50; height: 50; background-color: red; } </style> </head> <body> <div id="testdiv"></div> </body> </html>

jQuery .keypress & .keydown .which

对着背影说爱祢 提交于 2019-11-29 23:41:04
Ok so what is the difference in .keypress and .keydown/.keyup? At present I am using .keydown which returns a .which value of 38 for my key, now if i change it to .keypress it returns a value of 109 for that same key. What is the difference and why are the values different for the same key? RvdK If you press a button it fires a keydown and releasing it fires a keyup . The keypress usually comes between those two. keydown and keyup talk about which key has been changed. keypress tells which character that key represents. Note that this is all browser-dependent! See this article about the

Why doesn't OnKeyDown catch key events in a dialog-based MFC project?

╄→гoц情女王★ 提交于 2019-11-29 22:13:29
问题 I just create a dialog-based project in MFC (VS2008) and add OnKeyDown event to the dialog. When I run the project and press the keys on the keyboard, nothing happens. But, if I remove all the controls from the dialog and rerun the project it works. What should I do to get key events even when I have controls on the dialog? Here's a piece of code: void CgDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default AfxMessageBox(L"Key

Using addKeyListener to bind keys Javascript

痴心易碎 提交于 2019-11-29 18:15:10
I'm making a small game for a class and I am having trouble finding a way to bind keys to functions. One solution I found was using addEventListener, however I cannot find a way to take the key value and bind it to an animation. document.addEventListener("keydown", function(event){ if(event.which === 65) { $("#player").animate({left: '5px'}); } }); var x = event.which || event.keyCode; // Use either which or keyCode, depending on browser support Try to do it in jQuery way. jsfiddle.net/jpmvkqzh/ position.left of element keep increasing 5px when user pressing right arrow key and deceasing 5px

How do you detect simultaneous keypresses such as “Ctrl + T” in VB.NET?

我与影子孤独终老i 提交于 2019-11-29 18:05:35
问题 I am trying to detect the keys "Control" and "t" being pressed simultaneously in VB.NET. The code I have so far is as follows: Private Sub frmTimingP2P_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.T Then MessageBox.Show("Ctrl + T") End If End Sub I can detect one key or the other by removing the and statement and the second keyvalue statement, but I don't really get anything when I try this. Is there

keydown in c# doesn't work for some reason

寵の児 提交于 2019-11-29 15:34:11
I'm trying to do a calculator and all I have to do is make it work with the keyboard. This should work but it doesn't. private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.D1) { resultarea.Text = "fgdgd"; //number(1); } .... } Any idea what could have gone wrong? Edit: it's still not working. I figured I might aswell post the whole code. public partial class Form1 : Form { public Form1() { InitializeComponent(); KeyPreview = true; } double first = 0; double second = 0; string op = ""; bool last = true; public void calculate() { double aux = first; first = second;

Handling key events on WebBrowser control

自作多情 提交于 2019-11-29 14:55:28
问题 I am using the WebBrowser control in a C# application and want to handle all key events while the WebBrowser has the focus, regardless what individual content element (input field, link, etc.) is focused. I tried to simply add an event handler to browser controls KeyDown event, but this does not work. I don't want to explicitly hook a handler to each focusable HtmlElement . How can I receive all key events before they are passed to the browser or its content elements? 回答1: you have the

Firebug JS warning to jQuery 1.4.2 “The 'charCode' property of a keyup event should not be used. The value is meaningless.” To ignore it?

☆樱花仙子☆ 提交于 2019-11-29 14:46:42
Firebug 1.5.4 JavaScript warning : The 'charCode' property of a keyup event should not be used. The value is meaningless. To ignore it? Is there any issue? The warning appears to jQuery 1.4.2 keyup and keydown , not on keypress . I have read that on changing event.keyCode and event.charCode to event.which must fix it, but it does not work for me. Full code example in http://jsfiddle.net/zTevK/2/ and in question My code uses keyup and does not work with keypress . $(document).bind('keyup', function(e){ var key = e.which; if (key > 36 && key < 41) { if (key == 37) { changeTab(-1); } if (key ==

Javascript, key press value is always one character behind the latest?

廉价感情. 提交于 2019-11-29 13:33:45
If I type 'St', by the time I press the t, if I output the input of textfield.value in the onkeypress / onkeydown functions, I only get 'S'. Why is this? How do I get rid of this lag? use the keyup event instead of keypress . keydown will show the before-keystroke value, as will keypress (apparently). Within the keypress event, it's still possible to prevent the typed character from registering, so the input's value canot be updated until after the keypress event. You can use the keyup event instead, or use window.setTimeout() to set up a delay. Because the keystroke is not registered until