[Gtk-sharp-list] [Another Request] gdk/Key.cs modification

Charles Iliya Krempeaux charles@reptile.ca
17 Apr 2003 01:15:27 -0700


Hello,

On Wed, 2003-04-16 at 17:35, fd wrote:
> On Thu, 2003-04-17 at 01:19, Miguel de Icaza wrote:
> > Or Key_A and Key_a.  I think we just uncovered a nasty problem.
> 
> Agreed. Maybe something can be learnt from Windows.Forms; anyone
> familiar with how this is handled there?
> 
> I've found that in my code, matching the string tends to work fine (ie.
> if (key == 'a')).

The problem with using characters is that not every key, on
a keyboard, corresponds to a letter in the alphabet.  (Not
even if you are using Unicode.)

For example: the Num Lock key, the Page Up key, etc.

Also, what about keyboards, with keys that open up your web browser,
e-mail program, etc.  These cannot be represented by letters of the
alphabet.

So... sometimes a "key" will correspond to a letter of the alphabet.
And sometimes it won't.

What we need is a data structure that can accommodate this.  (In C
I'd use a union.)  For C#, maybe something like:


    public struct Key
    {
     	private uint code;

        public Key(uint c)
        {
            this.code = c;
        }



        private static char[] char2key;
        static Key()
        {
            // Initialize "char2key" here with a table
            // that will convert a "char" to a "key" value.
        }



        static public bool operator == (Key left, char right)
        {
            return (left == char2key[right]);
        }

	static public bool operator != (Key left, char right)
        {
            return (left != char2key[right]);
        }

	static public bool operator == (char left, Key right)
        {
            return (char2key[left] == right);
        }

	static public bool operator != (char left, Key right)
        {
            return (char2key[left] != right);
        }



        static public readonly Key PageUp   = new Key(0xFF55);
        static public readonly Key PageDown = new Key(0xFF56);
        // Etc...

    } // struct Key

To make it so you can do stuff like...

    Key key;

    if ('a' == key) {
        // Do something.
    }

(As you requested.)

Of course, this assumes that a character can only be generated by
one key.

And for the other stuff, you can do a:

    Key key;

    if ('a' == Key.PageUp) {
        // Do something.
    }


What do you think?


See ya

-- 
     Charles Iliya Krempeaux, BSc
     charles@reptile.ca

________________________________________________________________________
 Reptile Consulting & Services    604-REPTILE    http://www.reptile.ca/