ginerjm Posted March 18, 2013 Share Posted March 18, 2013 I"m using the following code to detect and interpret numeric inputs: function handleTimeEntry(event) { var obj = event.target||event.srcElement; var kycode = event.keyCode; var ky = String.fromCharCode(kycode); alert("got kycode of "+kycode+ " leading to ky of "+ky);blah,blah,blah } When I use the normal number keys - no problem. But when I use my numeric keypad at the end of my keyboard I get the following: (with num lock on): a 1 keypress returns a kycode = 97 and ky = a which my script rejects as non-numeric. Other keys do similar 'wrong' things. (with num lock off): a 1 keypress returns a kycode =35 and ky = # which is also rejected. According to the keycode charts I've read online, the kycode values I'm getting are correct with numlock on according to the stenciled characters that are on the keys I'm hitting, but the result of "fromCharCode(kycode)" is not what I should be getting. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/275828-trouble-with-keycode-handling/ Share on other sites More sharing options...
nogray Posted March 19, 2013 Share Posted March 19, 2013 Why not just use a regular expression to remove undesired values. This way you don't have to worry about the keycode, language, encoding, etc... e.g. alert('12 trees'.replace(/[^\d]/g, '')); Quote Link to comment https://forums.phpfreaks.com/topic/275828-trouble-with-keycode-handling/#findComment-1419472 Share on other sites More sharing options...
ginerjm Posted March 19, 2013 Author Share Posted March 19, 2013 As you see, I'm capturing individual keystrokes. The purpose is to monitor data entry and modify the input as it happens. Can regex be used for that purpose? Specifically I'm capturing day and time values and trying to do it all numerically to lessen the load on the person doing the input. So instead of having to type "Fri. 05:00" as an example, they need only type 10500 which gets modified as follows: 1 -> Fri. 0 -> Fri. 0 5 -> Fri. 05 0 -> Fri. 05:0 0 -> Fri. 05:00 The use of the numeric keypad is crucial in this situation - to not be able to handle it would be a great weakness in my appl. So getting back to my question - why does my JS not recognize the number keypad's inputs properly? Quote Link to comment https://forums.phpfreaks.com/topic/275828-trouble-with-keycode-handling/#findComment-1419568 Share on other sites More sharing options...
Solution ginerjm Posted March 19, 2013 Author Solution Share Posted March 19, 2013 OK - I changed the way I'm determinging the numeric value of my keyup result to use two arrays - one for normal number keycodes and another for the numeric keypad keycodes. My input is being processing properly now. Please see my new post on how to convert an Enter key to a tab keystroke if you would like to assist me there. Consider my problem closed. Quote Link to comment https://forums.phpfreaks.com/topic/275828-trouble-with-keycode-handling/#findComment-1419589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.