Tk Source Code

View Ticket
Login
Ticket UUID: 2945130
Title: {Shift,Control,Alt}_R press is answered by _L release
Type: Bug Version: obsolete: 8.5.7
Submitter: eserte Created on: 2010-02-03 08:52:28
Subsystem: 01. Bindings Assigned To: fvogel
Priority: 6 Severity: Minor
Status: Closed Last Modified: 2016-09-04 20:02:09
Resolution: Fixed Closed By: fvogel
    Closed on: 2016-09-04 20:02:09
Description:
Pressing the right Shift, Control, or Alt key on Windows (checked only with Tk 8.5.7) produces
correctly an event with a ..._R keysym. However releasing the
same key produces an event with a ..._L keysym. The same works as
expected on X11 systems.

The bug was originally reported for Perl/Tk. See
https://rt.cpan.org/Ticket/Display.html?id=47173

For your convenience, a test script is attached.

Regards,
    Slaven
User Comments: fvogel added on 2016-09-04 20:02:09:

For the records, here is the pointer about the discussion on Tcl Core list


fvogel added on 2016-09-04 18:49:56:
Following discussion on Tcl core:

- Fix merged for the right Control and Alt keys.

- Concerning the right Shift key: WONTFIX, due to Windows platform limitation.

fvogel added on 2016-09-02 06:24:16:

A request for comments on the solutions to this bug was posted on 23/08/16 on Tcl Core List.

No feedback was seen so far.

I'll give a tiny bit more time to this thinking and then I'll go for:

- merging the fix for Control and Alt keys
- WONTFIX for the Shift key


fvogel added on 2016-08-12 17:34:18:
I have a solution for the shift key in branch bug-2945130fff-shift.

It forces setting of the extended flag for the right shift key at event translation time, and this flag is used at event processing time to return the correct (left or right) keysym.

I don't like it so much: it changes the state of the event corresponding to pressing/releasing the shift key. The %s substitution content is changed.

(Note that this is also what happens for the right Control and Alt keys, but masking this flag for <KeyRelease> (while it was correctly set in %s for <KeyPress> was a mistake I believe).

Dubious about whether this proposed solution should be merged or if the Shift case should just not be fixed. Opinions, please ?

fvogel added on 2016-08-11 12:25:47:
Proposal for a solution now committed in branch bug-2945130fff.

This considers only the case of the Control and Alt keys for now.
The idea is to use the Extended flag returned by Windows when the right Control or Alt key is pressed or released. This flag is not raised by Windows when the left Control or Alt keys are pressed or released.

The case of the Shift key is different because Windows does not report the right Shift key with the extended flag.

Please comment about the proposed solution.

fvogel added on 2016-08-09 22:08:19:
Code snippet again, without damned loss of newlines:

    switch (keycode) {
	/*
	 * Windows only gives us an undifferentiated VK_CONTROL code (for
	 * example) when either Control key is pressed. To distinguish between
	 * left and right, we have to query the state of one of the two to
	 * determine which was actually pressed. So if the keycode indicates
	 * Control, Shift, or Menu (the key that everybody else calls Alt), do
	 * this extra test. If the right-side key was pressed, return the
	 * appropriate keycode. Otherwise, we fall through and rely on the
	 * keymap table to hold the correct keysym value.
	 */

    case VK_CONTROL:
	if (GetKeyState(VK_RCONTROL) & 0x80) {
	    return XK_Control_R;
	}
	break;
    case VK_SHIFT:
	if (GetKeyState(VK_RSHIFT) & 0x80) {
	    return XK_Shift_R;
	}
	break;
    case VK_MENU:
	if (GetKeyState(VK_RMENU) & 0x80) {
	    return XK_Alt_R;
	}
	break;
    }
    return keymap[keycode];

fvogel added on 2016-08-09 22:07:27:

The code to distinguish between left and right shift|control|alt keys for Windows is in tkWinKey.c, function KeycodeToKeysym(). It is the purpose of the following snippet:

switch (keycode) { /* * Windows only gives us an undifferentiated VK_CONTROL code (for * example) when either Control key is pressed. To distinguish between * left and right, we have to query the state of one of the two to * determine which was actually pressed. So if the keycode indicates * Control, Shift, or Menu (the key that everybody else calls Alt), do * this extra test. If the right-side key was pressed, return the * appropriate keycode. Otherwise, we fall through and rely on the * keymap table to hold the correct keysym value. */
case VK_CONTROL: if (GetKeyState(VK_RCONTROL) & 0x80) { return XK_Control_R; } break; case VK_SHIFT: if (GetKeyState(VK_RSHIFT) & 0x80) { return XK_Shift_R; } break; case VK_MENU: if (GetKeyState(VK_RMENU) & 0x80) { return XK_Alt_R; } break; } return keymap[keycode];

This code was added in response to [220399ffff], back in 2000. The corresponding artifact is [a70c9415ac7b0cf8].

The idea is to use the GetKeyState() Windows API function to query the state of one of the two (left and right) keys to distinguish which one was down *at the time the Windows message (WM_KEYDOWN for KeyPress, or WM_KEYUP for KeyRelease) was generated* by the OS.

This works therefore fine for KeyPress: the WM_KEYDOWN message is generated by the OS just *after* the key went down, and querying which (left or right) key was down at event generation time is OK to make the difference.

However for KeyRelease this does not work because the WM_KEYUP message is generated by the OS just *after* the key went up. At the time of message geenration this time, both (left and right) keys were already up and the querying logic using GetKeyState() can no longer provide the wanted information.

I have to think about how to fix this.


fvogel added on 2016-08-08 22:06:35:

Also reported as [1534455].


eserte added on 2010-02-03 15:52:28:

File Added - 361440: k.tcl

Attachments: