473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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().To Upper()))
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.

Nov 17 '05 #1
3 7881
On 30 Mar 2005 07:17:40 -0800, Tegdeep wrote:
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().To Upper()))
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.

--
Claudio Grazioli
http://www.grazioli.ch
Nov 17 '05 #2
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()]));
}

Nov 17 '05 #3

Something to think about: what happens if the user presses an uppercase
accented character?
--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
9
by: Andy Chalkley | last post by:
Hi I need to convert surname and firstname fields in a table from upper case to normal. There are about 10000 records. Typically: DE ANTONIO De Antonio CONROY, R W AND...
3
by: MHenry | last post by:
All the lower case "c" in my database table are now upper case "C" starting in January 2004. All prior data is fine. I just noticed this after installing a Microsoft update patch a couple of days...
1
by: kids_pro | last post by:
Hi there, I want to capture the last key press in a textBox. So I decided to use KeyDown event and from the KeyEventArgs I read the e.KeyCode But when I type small letter the e.KeyCode always...
4
by: Chris | last post by:
Hi, How can I programatically set Upper, Lower and Normal case on a label or text box controls? Thanks
10
by: John Salerno | last post by:
Can someone tell me what's happening here. This is my code: PUNCT_SPACE_SET = set(string.punctuation + string.whitespace) def filter_letters(original): return ''.join(set(original) -...
2
by: Tim_Mac | last post by:
hi, i'm referring to System.Globalization.TextInfo.ToTitleCase(string s) and it works fine if you pass in an all lower or mixed case string. however i am trying to make amends for data entry staff...
14
by: fniles | last post by:
In VB.NET 2005 can I check if a letter in a string is upper case or lower case ? For example: I have the following 2 lines: NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5...
1
by: Curious | last post by:
I'm working on a word replacement program in .NET. I have a list of words spelt in American English and need to replace them with translated British spelling while keeping the upper or lower cases...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.