How do I detect if a upper/lower case letter was pressed on the keyboard?

T

Tegdeep

Here's what I want to do:

I have a hash table which contains data associated to different keys.
The Hash keys are represented by a single character from the keyboard:
0-9, a-z, A-Z, and the other special characters.

I want my program to detect what key the user pressed from the keyboard
and get the associated data from the hash table (associated with the
key he pressed which is the same as the hash key). The user can also
decide to have capital letters (using shift or caps lock).

Here's what I have so far, but it doesn't work:

protected override void OnKeyDown(KeyEventArgs e)
{
if(e.Modifiers == Keys.Shift)
{
if(myHashTable.ContainsKey(e.KeyData.ToString().ToUpper()))
foobar(myHash[e.KeyData.ToString().ToUpper()]);
}
else
{
if(myHash.ContainsKey(e.KeyData.ToString().ToLower()))
foobar(myHash[e.KeyData.ToString().ToLower()]);
}
}

What this code does is that whenever the user presses on Shift, all
other keys are ignored until the function foobar is completely
executed.

Can anyone help me with this? I want to detect if a upper/lower case
letter was pressed and its value.

Thanks.
 
C

Claudio Grazioli

Here's what I want to do:

I have a hash table which contains data associated to different keys.
The Hash keys are represented by a single character from the keyboard:
0-9, a-z, A-Z, and the other special characters.

I want my program to detect what key the user pressed from the keyboard
and get the associated data from the hash table (associated with the
key he pressed which is the same as the hash key). The user can also
decide to have capital letters (using shift or caps lock).

Here's what I have so far, but it doesn't work:

protected override void OnKeyDown(KeyEventArgs e)
{
if(e.Modifiers == Keys.Shift)
{
if(myHashTable.ContainsKey(e.KeyData.ToString().ToUpper()))
foobar(myHash[e.KeyData.ToString().ToUpper()]);
}
else
{
if(myHash.ContainsKey(e.KeyData.ToString().ToLower()))
foobar(myHash[e.KeyData.ToString().ToLower()]);
}
}

What this code does is that whenever the user presses on Shift, all
other keys are ignored until the function foobar is completely
executed.

Can anyone help me with this? I want to detect if a upper/lower case
letter was pressed and its value.

Thanks.

I'm not sure if I understand you right: You just want to know if the user
pressed 's' or 'S' for example?

If so, use the KeyPress event instead of the KeyDown event. This event has
a KeyPressEventArgs parameter. KeyPressEventArgs has a property KeyChar. I
think that's what your looking for.
 
T

Tegdeep

It works.
Thanks a lot Claudio.

Here's what my code looks like now, as a reference for those reading
this post in the future.

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress (e);
if(myHashTable.ContainsKey(e.KeyChar.ToString()))
foobar(myHashTable[e.KeyChar.ToString()]));
}
 
M

Mihai N.

Something to think about: what happens if the user presses an uppercase
accented character?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top