Re: Windows.forms.keys collection

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Sat, 12 May 2007 16:05:02 -0700, Oguzhan Filizlibay <someemail@xxxxxxxxxxxxxx> wrote:

Hi all,

I have the below code for sending each character in a string as a Keys.x enumeration. I've found that when I convert the char, it does not match the enumeration item index and thereby generates a very random output. Any ideas on how I can convert chars to their right equivalent in the Keys enumeration.

If you are going to do that, you need to correctly map the characters to key press events. The code you posted doesn't do that (obviously :) ).

The "Keys" enumeration is for actual keys on the keyboard. They correspond to physical keys, not ASCII characters. It is, in some sense, simply coincidental that a key value corresponds to any ASCII value (though obviously in another sense, that's by design).

Provided you are dealing only with alphanumeric characters, you can use code like what Brian suggests (his code looks to me as though it will handle the digits fine as well...Char.ToUpper() will get called for numbers, but should leave the character unchanged). I think it's safe to assume that the Keys enumeration isn't going to change the assignments for those values. But there are other keys in the enumeration that don't correspond to any ASCII character, and other ASCII characters cannot be converted simply by changing the case and applying a modifier key.

If you want to handle other characters, you may find it better to just use a table-lookup. You can build a Dictionary using ASCII values as the keys, and Keys values with the key code and modifiers pre-combined as the values. You can even populate the Dictionary for the alphanumerics with some simple loops that handle the numbers, upper case, and lower case characters, adding the other data in a more explicit manner (with a hard-coded array containing the desired key/value pairs, or storing them in a resource, or in the applications Settings as an "Application" domain setting).

Then for each character you want to convert, you just look it up in the Dictionary and use the resulting value.

Hope that helps.

Pete
.



Relevant Pages

  • Re: WS4HTM.BAS
    ... Katzy wrote: ... Is it because you indeed use these keys? ... to see your table with the "boxing characters" and the ALT keys ... graphs using ASCII characters that could then be imported into your ...
    (comp.os.cpm)
  • Re: Where to put interface?
    ... > 1) You need your device dependent API which returns characters. ... Both have peculiarities (ie, arrow keys, ... > 2) You should have a second level which translates your generic ... The most common output is as you described -- displaying the map, ...
    (rec.games.roguelike.development)
  • Re: The Last Remnant
    ... Smallish 3rd person characters rushing around with the screen flashing ... One thing I thought was poor design is that you can control the 3rd person ... of as RPG. ... that even help any if one knew exactly what all those keys are for? ...
    (comp.sys.ibm.pc.games.rpg)
  • Re: Great SWT Program
    ... failing of the GUI user-interface paradigm. ... And that probably works fine if the remote files aren't too big, ... the keys used for movement and cut, copy, paste can't even be depended ... characters and then type in the resulting number before hitting my ...
    (comp.lang.java.programmer)
  • Getting the correct size of a glyph in a font
    ... the text for the differing keys. ... however I found that those characters which have a descent ... GLYPHMETRICS and use the origin and its blackboxy, ...
    (microsoft.public.win32.programmer.gdi)