473,401 Members | 2,146 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,401 software developers and data experts.

Detecting if ASCII Control Characters are pressed (C#)

I am seeking a method to detect if ASCII Control Characters are pressed. My code currently shown here is not working too well i suspect.

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Transmitted_KeyDown(object sender, KeyEventArgs e)
  3. {
  4.           if (e.Modifiers==Keys.Control)
  5.            {
  6.                test = (char)e.KeyCode;              
  7.            }
  8. }
  9.  
Oct 10 '09 #1
10 8509
tlhintoq
3,525 Expert 2GB
"ASCII control characters" such as an ASCII 05 "Enquiry", or an ASCII 23 "End of transmission block" ??

How would someone type one of these?
Oct 10 '09 #2
ASCII 05 is Ctrl+E and ASCII 23 is Ctrl+W as seen from http://nemesis.lonestar.org/referenc...des/ascii.html.

I would like to find a more elegant way to do this rather than manually defining each block like this :

Expand|Select|Wrap|Line Numbers
  1. if (e.KeyData == (Keys.Control | Keys.A))
  2.             {
  3.                 test = (char)01;
  4.  
  5.  
  6.             }
  7.  
  8.             if (e.KeyData == (Keys.Control | Keys.B))
  9.             {
  10.                 test = (char)02;
  11.  
  12.  
  13.             }
  14.  
  15.  
  16.             if (e.KeyData == (Keys.Control | Keys.C))
  17.             {
  18.                 test = (char)03;
  19.  
  20.  
  21.             }
  22.  
Oct 11 '09 #3
tlhintoq
3,525 Expert 2GB
How do you feel about this logic? (suedo code, you can flush out)

On keypress

if control key pressed
{
switch e.KeyData
{
Case E
...
Case W
...
Case R
...
}
else
{
// No control key so we don't care
}
}
Oct 11 '09 #4
GaryTexmo
1,501 Expert 1GB
Out of curiosity, do you need this globally (from outside the application), or just inside the particular control?
Oct 11 '09 #5
Not exactly what I was looking for. I would prefer grabbing the KeyCode of the keypress and then putting it in a char.
Oct 11 '09 #6
@GaryTexmo
Just inside the particular control (textbox).
Oct 11 '09 #7
GaryTexmo
1,501 Expert 1GB
I tried a google search and came up with this...
http://www.java2s.com/Code/CSharp/Ev...Keypressed.htm

You don't need to worry about the ASync method in there. The particular lines of code that should interest you are...

Expand|Select|Wrap|Line Numbers
  1.         lbl.Text = "Key Down: " + e.KeyValue.ToString();
  2.         lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
  3.         lbl.Text += "\nKey Data: " + e.KeyData.ToString();
... which would be in your KeyDown event.

Does that help any?
Oct 11 '09 #8
@GaryTexmo
Not exactly, I already know about key modifiers, codes , values and such. The challenge is simplifying my existing code and enhancing it to include all keyboard modifiers.
Oct 11 '09 #9
GaryTexmo
1,501 Expert 1GB
Like what? I tried that little demo program on the site and it was trapping ctrl-e, which is one of the things you wanted.

Oooh, you mean you want the actual ascii value of it? I see... well that changes everything :D Sorry for the confusion.

I'm at a loss here. I tried a few console programs in C# and QBasic and I was able to get the functionality you want by reading the input stream, but I can't figure out how to do it on a form. I even tried some code I had that would read a global input stream that used all sorts of dllimports thinking maybe there was something in there, but it returns the same as the event arguments.

At this point, I think I agree with Thlintoq's method. While it's not exactly what you wanted, it will give you what you're looking for.
Oct 11 '09 #10
guimel
7
It looks like the KeyValue of the event is the Ascii value of the key (in uppercase), thus the following works:
Expand|Select|Wrap|Line Numbers
  1. if (e.Modifiers == Keys.Control && e.KeyValue >='A' && e.KeyValue <='Z')
  2.     int test = e.KeyValue - 'A' + 1;
  3.  
Be aware that this relies on undefined behaviour as it's never specified what the KeyValue of a specific key is.

Edit:
To make it a bit safer, you could have:
Expand|Select|Wrap|Line Numbers
  1.     int test = e.KeyValue - (int)Keys.A + 1;
  2.  
That's guaranted to work as long as the values of Keys.A to Keys.Z are all consecutive and in the right order in the Keys enum.
Oct 11 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Pratap Das | last post by:
Hi All, I am writing sort of a shell in Perl where I am trying to implement command recall at shell prompt like you have for zsh/tcsh. So when I press up/down arrow keys I should be able to get...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
0
by: JohnLongHorn97 | last post by:
Hello everyone, I would like to write a script that will read several ascii characters directly into the STDIN, so it will be like I pressed these values in my keyboard, in another window. for...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a KeyDown event in which I would like to determine if the current key is a standard character (i.e., from the ASCII character set). In C I would use something like the isascii...
44
by: Pilcrow | last post by:
Is there a way that a proram can detect whether it is operating in an ASCII or an EBCDIC environment?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.