1

Is there some kind of function which can calculate how many keystrokes are needed to type a specific character?

For example in french, the character Ÿ needs 3 keystrokes to be typed. Right now I'm just checking each character against an array and add the associated value, for Ÿ it would be 3.

Is there some way in PHP to automate this or do I have to stick with the array approach?

Christian Strang
  • 8,470
  • 5
  • 42
  • 53
  • 2
    on what keyboard layout? – Gordon Jan 31 '12 at 14:52
  • 1
    Minimum number of keystrokes? Because you can type Ÿ in 4 keystrokes too. And that's not even taking into account keyboard layout... – Ry- Jan 31 '12 at 14:52
  • 1
    I would be surprised if an actual library that did this would exist. Also, the answer depends on the keyboard and on the operating system. – JJJ Jan 31 '12 at 14:53
  • Also depends on accessibility - could be using an on-screen keyboard and clicking, in which case it's technically 0 keystrokes. – Marc B Jan 31 '12 at 14:55

5 Answers5

2

No not possible. PHP is serverside and doesn't know the keyboard layout of the user (client).

German people (with their keyboard layout) can do for example a ü with one key I think. But I don't even know how to type it.

So it is safe to say that: some char !== specific number of keystrokes.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
2

It is not possible to do this automatically as PHP is a server side script and cannot see the keyboard layout of the user, nor can it detect when a key is pressed. Therefore, your only solution would be to use a library, of which I do not believe exists.

You will need to proceed with creating your own library (array) of keystrokes. However, you could do this much faster if you integrated a temporary javascript script.

Make a list of all the characters you want (on paper or computer, doesn't matter), then feed them into a webpage that picks up your key strokes using javascript. Then use javascript to add these to an array which you can copy and paste into PHP.

Very easy and would save you a lot of time

Let me know if you need help with the JS

Update

This may help you with Windows and Mac, you will need a separate array for each OS and keyboard layout (detect the keyboard layout using javascript).

http://www.nouilles.info/keyboard_shortcuts.html

Update 2

What you will need to do is apply a condition to determine which array to use.

Set up the array like this:

$keystrokes = array(
     'mac'=>array(     // For mac users
          'english-us'=>array(
               'character'=>3
          )
     ),
     'windows'=>array(     // For windows users
          'english-uk'=>array(
               'character'=>2
          )
     )
);

And then have a default set for unknown layouts or OS's. I would advise setting Windows english-us as the default set as this is what is most used around the world.

Please note: There is no way to get this 100% accurate as there are so many different OS's and keyboard layouts it is simply far to time consuming to cover every possible combination. You might want to ask yourself if this functionality is absolutely necessary because it seems that it will be pretty unreliable.

Ben Carey
  • 16,540
  • 19
  • 87
  • 169
1

No, it's completely impossible to do automatically. There is no PHP library out there that will do that for you.

For example, I'm typing on Ubuntu right now. On Windows, I would use Alt+0233 to type an e with an acute accent. Right now I just copy and paste it. I could do that with the context menu. No keystrokes. Depending on your purpose, this is either:

  1. Completely impossible in PHP or
  2. There's definitely nothing built-in.
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

It's perfectly possible to do this with the help of JavaScript but it requires some effort.. You probably would want to do it all in JavaScript, though. First, start of by detecting the keyboard layouts language. Then send the detected language with AJAX or similar (alternatively save to a cookie) to a PHP script which somehow saves (session, database, etc) the layouts language.

Then, you could manually store the number of keystrokes required for each needed language in an array, i.e:

$keyboard['swedish'] = array(
  'a' => 1,
  'b' => 1,
  'ü' => 2
);

And when calculating the number of keystrokes needed for a character, you could do:

$keystrokesNeeded = $keyboard[$user['keyboardLayout']]['ü']; //If layout is swedish, it'd return 2

As said, requires some effort - but it's do able.

Community
  • 1
  • 1
Zar
  • 6,786
  • 8
  • 54
  • 76
  • If that's the purpose it might be better to just do the whole thing in JavaScript... – Ry- Jan 31 '12 at 15:06
  • @PeeHaa I'd say that it's another question - detecting if something was typed or pasted. There's on screen too, I can't see why the OP would want this - but it's at least partially solving his question. – Zar Jan 31 '12 at 15:06
  • but I try to get rid of the array approach, so that I don't have to know for each character how many keystrokes are necessary. But I guess thats not really possible/does not exist internally? – Christian Strang Jan 31 '12 at 15:07
  • @minitech I agree with you. However, the goal was to do it, atleast partially with PHP. – Zar Jan 31 '12 at 15:08
  • @ChristianStrang No, that's not possible, unless you use a library - which does the same thing with arrays. Haven't herd of such library though. – Zar Jan 31 '12 at 15:09
-1

It depends on which keyboard. Some keyboards will have shortcuts. You may have to write on your own, that is not a difficult to do. Take some commonly used keyboard and figure it out.

user1162286
  • 81
  • 1
  • 7
  • How would you make PHP detect what keyboard layout that is used? – Zar Jan 31 '12 at 15:11
  • PHP is server side language. One cannot detect what the client keyboard is using php, In the question he/she not mentioned that he would be using to detecting client keyboard. The question may be for using it for some other purpose – user1162286 Jan 31 '12 at 15:17