<p>Paraphasing <a href="http://forum.world.st/Windows-cog-vm-Keyboard-events-related-about-keycode-mapping-td4330359.html" rel="nofollow">http://forum.world.st/Windows-cog-vm-Keyboard-events-related-about-keycode-mapping-td4330359.html</a></p>
<blockquote>
<p><strong>guillep:</strong>  I was playing to add Function Key support in the windows vm (yep, always the same :P), and looking at the code, I saw this in the recordKeyboardEvent:</p>
</blockquote>
<pre><code>  evt = (sqKeyboardEvent*) sqNextEventPut();
...
  evt->charCode = keymap[keyCode & 0xff];
</code></pre>
<blockquote>
<p>the problem with that line is that KeyDown and KeyUp events send VirtualKeycodes as keycodes and the Char event sends a unicode char value.  And, it makes collisions, since for example:<br>
-- $p char value is 112 ; and<br>
-- F1 virtual code value is 112 too</p>
</blockquote>
<p>And other keys are the same, image-side having the same keycode with different keys.<br>
To distinguish these keys, the following provides to the image a Char event the keycode without mapping (<a href="https://github.com/OpenSmalltalk/opensmalltalk-vm/blob/aed5e3391301011cc6b9ee6a353ee563f4ab6dbd/platforms/win32/vm/sqWin32Window.c#L1319-L1337">code link for more context</a>):</p>
<div class="highlight highlight-source-diff"><pre>    case WM_CHAR:
    case WM_SYSCHAR:
      /* Note: VK_RETURN is recorded as virtual key ONLY */
      if(keyCode == 13) return 1;
<span class="pl-mi1"><span class="pl-mi1">+</span>      charCode = keyCode;</span>
      pressCode = EventKeyChar;
     break
...
<span class="pl-md"><span class="pl-md">-</span>  evt->charCode = keymap[keyCode & 0xff];</span>
<span class="pl-mi1"><span class="pl-mi1">+</span>  evt->charCode = charCode? charCode : keymap[keyCode & 0xff];</span></pre></div>
<blockquote>
<p><strong>andreas:</strong> The change makes no sense (it will break most non-ascii input like accents, umlauts, etc). You really shouldn't be using character events for handling function keys. There is no 'F11 Character' in any character encoding world-wide so trying to represent F11 as a character is completely futile. You need to use keyDown and keyUp events, since F11 & friends are KEYs not CHARACTERs.</p>
</blockquote>
<blockquote>
<p><strong>guille:</strong> Sure! I'm not generating Char events for F11 ;).  Since F11 does not generate Char events.<br>
The problem is that the p key generates the same KeyDown event than F11 for the vm.  I'm only fixing that p-key and F1-key have different keyCodes for KeyDown and KeyUp events.<br>
Now, utf32Code should be only used on KeyChar events, and I don't care about them.<br>
.<br>
How does it work now?<br>
(KeyDown, KeyUp) and KeyChar events send different keyCodes to the image.<br>
Right now works because in no place in the image keyDown: is handled.<br>
.<br>
More on, in the unix vm, the behavior is the following:<br>
when pressing F1:<br>
-- key: 112 char: 112 type: keyDown<br>
-- key: 112 char: 112 type: keyUp<br>
when pressing p:<br>
-- key: 112 char: 112 type: keyDown<br>
-- key: 112 char: 112 type: keystroke<br>
-- key: 112 char: 112 type: keyUp<br>
when pressing shift + p (P):<br>
-- key: 254 char: 254 type: keyDown<br>
-- key: 80 char: 80 type: keyDown<br>
-- key: 80 char: 80 type: keystroke<br>
-- key: 80 char: 80 type: keyUp<br>
After adding support for Function keys, when pressing F1:<br>
key: 16 char: 16 type: keyDown<br>
key: 16 char: 16 type: keyUp<br>
.<br>
The question is: Should KeyDown, KeyUp and KeyChar events for the same key produce the same keyCode?  I think yes.  Because the keyCode indicates the key pressed in the keyboard. Doesn't it?</p>
</blockquote>
<blockquote>
<p><strong>andreas:</strong> It does. Which is precisely the reason why it <em>can't</em> produce different values when you press the shift key with it. The KEY you are pressing does not change depending on the modifier; your OS decides that the combination of the Shift key and the P key produces an uppercase P character.</p>
</blockquote>
<blockquote>
<p><strong>guille:</strong> The main problem is that I want keycodes to be the same through the three platforms (unix, mac and windows).  And for that, I have to do a mapping somewhere (this piece of code I wrote today was to play and see how it behaves, because the codes are to be defined :) ).<br>
So, I thought those conversions could be done through the keymap array.<br>
Maybe that's not the way to do the mapping, but the mapping should be done somewhere.</p>
</blockquote>
<blockquote>
<p><strong>andreas:</strong> There was general agreement that probably the best way to do this would be based on the X11/keysym.h because it seems to be the most complete source for key codes. It's just that that nobody sat down and wrote the mapping from VK_XXX to XK_YYY.</p>
</blockquote>
<blockquote>
<p><strong>guillep:</strong> Hmm, that would be good, I'll have a look tomorrow :).  Thanks!</p>
</blockquote>
<p>What was the conclusion of looking at that <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=708322" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/guillep">@guillep</a>?</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/396?email_source=notifications&email_token=AIJPEW3PUXQ7U5KKAHAONVLP2EM7BA5CNFSM4HP46HZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXQ54YI#issuecomment-501341793">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AIJPEW5SOG2BLSQYKOOMDDLP2EM7BANCNFSM4HP46HZA">mute the thread</a>.<img src="https://github.com/notifications/beacon/AIJPEW5MIP5S6LHCX6BLGXLP2EM7BA5CNFSM4HP46HZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXQ54YI.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/396?email_source=notifications\u0026email_token=AIJPEW3PUXQ7U5KKAHAONVLP2EM7BA5CNFSM4HP46HZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXQ54YI#issuecomment-501341793",
"url": "https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/396?email_source=notifications\u0026email_token=AIJPEW3PUXQ7U5KKAHAONVLP2EM7BA5CNFSM4HP46HZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXQ54YI#issuecomment-501341793",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>