Bug 6707 - KeyEventArgs constructor incorrectly adds modifier keys
Summary: KeyEventArgs constructor incorrectly adds modifier keys
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: master
Hardware: All All
: Lowest normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-08-27 11:54 UTC by Ivo Smits
Modified: 2013-03-08 16:26 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Suggested patch (3.63 KB, patch)
2012-08-27 14:15 UTC, Ivo Smits
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Ivo Smits 2012-08-27 11:54:21 UTC
Description of Problem:
The KeyEventArgs constructor gets the current state of the modifier keys and combines this with the specified keydata value. This seems incorrect:
- This behaviour is not documented by Microsoft
- Constructing KeyEventArgs(Keys.None) will give different values for KeyEventArgs.KeyData on MS .Net and Mono (on Mono the value depends on the modifier keys currently pressed, while on MS .Net it does not)
- Querying the current state of modifier keys will trigger initialization of XPlatUI, which will fail at least on Linux when no X server is available.

Steps to reproduce the problem:
The following code will produce different results on MS .Net and Mono when any of the modifier keys (Ctrl, Shift, Alt) is pressed when the form is focused:

using System;
using System.Windows.Forms;
class Program {
	static void Main(string[] args) {
		Timer timer = new Timer();
		timer.Interval = 100;
		timer.Tick += Tick;
		timer.Enabled = true;
		Application.Run(new Form());
	}
	static void Tick(Object sender, EventArgs e) {
		KeyEventArgs e2 = new KeyEventArgs(Keys.None);
		Console.WriteLine(e2.KeyData.ToString());
	}
}


Additional Information:
I will try to create a patch to move the code that queries the modifier keys out of the KeyEventArgs constructor.
Comment 1 Ivo Smits 2012-08-27 14:15:12 UTC
Created attachment 2419 [details]
Suggested patch
Comment 2 Miguel de Icaza [MSFT] 2013-03-08 16:26:19 UTC
Thanks, applied the pull request.