What's an easy way to add Keys programatically?
-
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
-
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
Is there a Keys.FromChar or something ? Is Keys an enum and Enum.Parse would do it ?
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
Well, if your in a Console Application then you can use
ReadKey
which can tell you the key that was pressed.ConsoleKey thing = Console.ReadKey(true).Key;
The
true
means that the key the user pressed will not be printed onto the screen. If you're in a Windows Application then set the formsKeyPreview
property totrue
and use theKeyPress
event which will have data on the key that was pressed.My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
You want to use the Enum.Parse static method
public static object Parse( Type enumType, string value, bool ignoreCase );
Applying this method to your example goes like this:key = (Keys)Enum.Parse(typeof(Keys), input, true);
Don't forget to sanitize the input value. -
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
-
It doesn't always work (Unicode & foreign languages are the trickiest) but you can simply cast them:
key = (Keys)input;
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
I tried this and got
error CS0030: Cannot convert type 'string' to 'System.Windows.Forms.Keys'
-
I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:
if (input == "a")
{
key = Keys.A;
}
// ... keep goingI am sure there is an easier way to do this. Can someone help me out? Thanks in advance.
Thanks, it works.
-
I tried this and got
error CS0030: Cannot convert type 'string' to 'System.Windows.Forms.Keys'