0

What would be the process for converting basic alphanumeric character into javascript event key codes?

For example the letters sso would convert to 83, 83, 79


The idea is we've got a CMS field which somebody can enter a key sequence like hello, this is stored in a db which is retrieved by PHP and displayed in a variable via a php template.

Then in javascript we monitor the keypress event to check if that sequence is entered via a event listener.

So we need to be able convert the string into keycodes which we can monitor in the event.

I've tried using ord in like

$letters = str_split($str);
$sequence = array();
foreach ($letters as $letter) {
    $sequence[] = ord($letter);
}
dd($sequence);

but that produces

array:3 [▼
  0 => 115
  1 => 115
  2 => 111
]

So I assume thats just converting it to ASCII, how would we actually get the keycode value?

Thanks

chris85
  • 23,846
  • 7
  • 34
  • 51
owenmelbz
  • 6,180
  • 16
  • 63
  • 113
  • See https://api.jquery.com/keydown/, simple demo: https://jsfiddle.net/cj2nj6x8/ Not really a PHP question though. – chris85 Jan 07 '17 at 19:53
  • Why not do the reverse and turn the keycodes into characters? I'm not entirely sure what you gain from looking at the keycodes themselves. – VLAZ Jan 07 '17 at 20:06
  • You can store the key sequence as a string and use javascript to convert to keycodes. – DonovanM Jan 07 '17 at 21:09
  • @chris85 - sorry thats not what I asked, I know how to do the javascript, I'm asking how to use PHP to convert a string into keyCodes for the event. – owenmelbz Jan 07 '17 at 21:28
  • @vlaz - because we cannot expect CMS users to type in javascript keycodes – owenmelbz Jan 07 '17 at 21:28
  • @DonovanM - I spose we could do the conversion in JS rather than PHP, but would still like to know how, rather than a workaround – owenmelbz Jan 07 '17 at 21:28
  • @Owen your CMS user will type in "hello", but in your event listener you'd translate **72** into **h**, **69** into **e**, etc. Again, I don't see what you gain by translating a string into the codes themselves. – VLAZ Jan 07 '17 at 21:36
  • @vlaz - the event listener isn't used within the CMS, its used in a angularjs application elsewhere. Which is why we're storing the string in the database which will be used later - its also used for display purposes and other reasons. Thats why we want the original data - then convert it into keycodes when its needed to be – owenmelbz Jan 07 '17 at 21:39
  • @Owen I took a look around and I don't see a reliable way of doing that. It is an interesting way of doing things but it seems unnecessarily more difficult and potentially error prone than using javascript alone. Converting with javascript guarantees that it'll be correct. Keycodes aren't always the same even between different browsers – DonovanM Jan 07 '17 at 21:43
  • I really do not understand the problem, in that case. I thought you had some string that you want to look for when somebody is typing, so I wasn't sure why you need the key codes at all. Now, I don't even know what exactly are you doing. Still not sure why the key codes are relevant, however it just sounds like an XY problem to me. – VLAZ Jan 07 '17 at 21:46
  • @vlaz - All I was after was a way to convert alphanumeric characters into javascript keycodes via PHP. The reasoning is all irrelevant :) – owenmelbz Jan 07 '17 at 21:51
  • 1
    @Owen I think it is. For one, a _key code_ does not necessarily mean the same _letter_. If I were to type in the sequence "hello" in English with the English layout, then you'd get `72`, `69`, `76`, `76`, `79` HOWEVER, if I were to switch my layout to, say, Cyrillic or something you would get _different_ letters for the same key codes. You can also get key code, say, 98 and you would have no idea if it's `2` or simply down, since it corresponds to a numeric pad key. So, if your end goal is to match a string, then you shouldn't be relying on the key codes but on what the person actually typed. – VLAZ Jan 07 '17 at 22:00
  • You could write your own keyCode translator with [this list](http://stackoverflow.com/a/23377822/3233827) as input. I don't think there is a predefined function or library in PHP. – ssc-hrep3 Jan 08 '17 at 13:43

0 Answers0