473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(KeyEv entArgs e)
{
if(e.Modifiers == Keys.Shift)
{
if(myHashTable. ContainsKey(e.K eyData.ToString ().ToUpper()))
foobar(myHash[e.KeyData.ToStr ing().ToUpper()]);
}
else
{
if(myHash.Conta insKey(e.KeyDat a.ToString().To Lower()))
foobar(myHash[e.KeyData.ToStr ing().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 7999
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(KeyEv entArgs e)
{
if(e.Modifiers == Keys.Shift)
{
if(myHashTable. ContainsKey(e.K eyData.ToString ().ToUpper()))
foobar(myHash[e.KeyData.ToStr ing().ToUpper()]);
}
else
{
if(myHash.Conta insKey(e.KeyDat a.ToString().To Lower()))
foobar(myHash[e.KeyData.ToStr ing().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 KeyPressEventAr gs parameter. KeyPressEventAr gs 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(KeyP ressEventArgs e)
{
base.OnKeyPress (e);
if(myHashTable. ContainsKey(e.K eyChar.ToString ()))
foobar(myHashTa ble[e.KeyChar.ToStr ing()]));
}

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
20466
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 pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey its me since I am getting frustrated. but anyway here's a sample code. while (1) { if...
9
2627
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 B L Conroy, R W and B L BURKE Burke SMITH Smith
3
1704
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 ago. Any ideas what happened, or how to fix? I thought I could change every letter C to lower case, and then change every c that starts a word to an upper case C, but I don't know how to do the second step.
1
5496
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 return upper case letter. Is there any configuration so that I can capture the correct letter case? Also in Vb I can change KeyAscii to another value. How can I do that in C#? Regards,
4
1788
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
2363
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) - PUNCT_SPACE_SET)
2
7467
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 who like to leave Caps Lock on all the time, so all the input is in upper case. ToTitleCase("CORK") returns "CORK" instead of "Cork". any ideas why? i can obviously sent all the strings ToLower before calling ToTitleCase but that seems like a...
14
43841
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 NQ,z2003,11/10/2003,223801,260154 NQ,H2004,11/11/2003,1416.5,1422.5,1406.5,1415 NQ,h2004,11/10/2003,56,1191 Notice the 1st line has uppercase Z (Z2003) and the 2nd line has lowercase z
1
2517
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 of each letter in these words. For instance, I have a dictionary below: theater theatre center centre annualized annualised
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.