473,396 Members | 1,804 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,396 software developers and data experts.

Changing KeyCode in OnKeyDown

Z
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
to change it to another unicode key from a different code page (e.g.,
"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
are all only getters. They are read only and thus do not permit changing the
assigned KeyCode. My question is this: How do I change the KeyCode of the
KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
an instance of Keys, and THAT constructor takes no argument. Even if I create
an instance of Keys, then I'm faced with the read only nature of KeyCode,
KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.
Jul 21 '05 #1
6 3868
The Keys required by KeyEventArgs is an enumerated type, you don't use
the constructor. For example, to simulate pressing Enter:
new KeyEventArgs(Keys.Enter)

I don't think you'll be able to do your code page translation with the
KeyEventArgs class.

Do you have to use OnKeyDown? Would OnKeyPress meet your needs?

protected override void OnKeyPress(KeyPressEventArgs e)
{
// perform your logic to get the key from another codepage
Char newChar = translateChar(e.KeyChar);
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Joshua Flanagan
http://flimflan.com/blog
Z wrote:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
to change it to another unicode key from a different code page (e.g.,
"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
are all only getters. They are read only and thus do not permit changing the
assigned KeyCode. My question is this: How do I change the KeyCode of the
KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
an instance of Keys, and THAT constructor takes no argument. Even if I create
an instance of Keys, then I'm faced with the read only nature of KeyCode,
KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.

Jul 21 '05 #2
Z
Thanks, Joshua, for your suggestion. This does work as you say. One problem
remains, and that is that the cursor (or caret) does not advance past the
newly inserted character. Guess I'll now have to work on developing a custom
Caret class that I will control via code. Thanks again for your help.
"Joshua Flanagan" wrote:
The Keys required by KeyEventArgs is an enumerated type, you don't use
the constructor. For example, to simulate pressing Enter:
new KeyEventArgs(Keys.Enter)

I don't think you'll be able to do your code page translation with the
KeyEventArgs class.

Do you have to use OnKeyDown? Would OnKeyPress meet your needs?

protected override void OnKeyPress(KeyPressEventArgs e)
{
// perform your logic to get the key from another codepage
Char newChar = translateChar(e.KeyChar);
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Joshua Flanagan
http://flimflan.com/blog
Z wrote:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
to change it to another unicode key from a different code page (e.g.,
"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
are all only getters. They are read only and thus do not permit changing the
assigned KeyCode. My question is this: How do I change the KeyCode of the
KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
an instance of Keys, and THAT constructor takes no argument. Even if I create
an instance of Keys, then I'm faced with the read only nature of KeyCode,
KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.

Jul 21 '05 #3
Z
Actually, on closer inspection, it does not work. I still had remnants of
code left in OnKeyDown and that was getting in the way. In OnKeyDown I can
call a method that accepts the character I want to display instead of the
character that was typed, and that method just concatenates that new char to
the existing chars already in the TextBox. Although this does work, the
cursor (as I mentioned) does not advance. But calling
base.OnKeyPress(newchar) does not render the new character. The typed
character is the one that shows up.

"Z" wrote:
Thanks, Joshua, for your suggestion. This does work as you say. One problem
remains, and that is that the cursor (or caret) does not advance past the
newly inserted character. Guess I'll now have to work on developing a custom
Caret class that I will control via code. Thanks again for your help.
"Joshua Flanagan" wrote:
The Keys required by KeyEventArgs is an enumerated type, you don't use
the constructor. For example, to simulate pressing Enter:
new KeyEventArgs(Keys.Enter)

I don't think you'll be able to do your code page translation with the
KeyEventArgs class.

Do you have to use OnKeyDown? Would OnKeyPress meet your needs?

protected override void OnKeyPress(KeyPressEventArgs e)
{
// perform your logic to get the key from another codepage
Char newChar = translateChar(e.KeyChar);
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Joshua Flanagan
http://flimflan.com/blog
Z wrote:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
to change it to another unicode key from a different code page (e.g.,
"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
are all only getters. They are read only and thus do not permit changing the
assigned KeyCode. My question is this: How do I change the KeyCode of the
KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
an instance of Keys, and THAT constructor takes no argument. Even if I create
an instance of Keys, then I'm faced with the read only nature of KeyCode,
KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.

Jul 21 '05 #4
This worked for me

protected override void OnKeyPress(KeyPressEventArgs e) {
//translate your character - I use ToUpper just for an example
Char newChar = Char.ToUpper(e.KeyChar);
this.Text += newChar;
// set the caret to the end
this.SelectionStart = this.Text.Length;
// tell the rest of the event chain there is no further work to do
e.Handled = true;
// I'm not sure if this line is necessary. it works without it
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Z wrote:
Actually, on closer inspection, it does not work. I still had remnants of
code left in OnKeyDown and that was getting in the way. In OnKeyDown I can
call a method that accepts the character I want to display instead of the
character that was typed, and that method just concatenates that new char to
the existing chars already in the TextBox. Although this does work, the
cursor (as I mentioned) does not advance. But calling
base.OnKeyPress(newchar) does not render the new character. The typed
character is the one that shows up.

"Z" wrote:

Thanks, Joshua, for your suggestion. This does work as you say. One problem
remains, and that is that the cursor (or caret) does not advance past the
newly inserted character. Guess I'll now have to work on developing a custom
Caret class that I will control via code. Thanks again for your help.
"Joshua Flanagan" wrote:

The Keys required by KeyEventArgs is an enumerated type, you don't use
the constructor. For example, to simulate pressing Enter:
new KeyEventArgs(Keys.Enter)

I don't think you'll be able to do your code page translation with the
KeyEventArgs class.

Do you have to use OnKeyDown? Would OnKeyPress meet your needs?

protected override void OnKeyPress(KeyPressEventArgs e)
{
// perform your logic to get the key from another codepage
Char newChar = translateChar(e.KeyChar);
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Joshua Flanagan
http://flimflan.com/blog
Z wrote:

I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
to change it to another unicode key from a different code page (e.g.,
"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
are all only getters. They are read only and thus do not permit changing the
assigned KeyCode. My question is this: How do I change the KeyCode of the
KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
an instance of Keys, and THAT constructor takes no argument. Even if I create
an instance of Keys, then I'm faced with the read only nature of KeyCode,
KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.

Jul 21 '05 #5
Z
Thanks, Joshua. I will give it a try and report back. I have to go teach a
class right now. P.S. Enjoyed your web site.

"Joshua Flanagan" wrote:
This worked for me

protected override void OnKeyPress(KeyPressEventArgs e) {
//translate your character - I use ToUpper just for an example
Char newChar = Char.ToUpper(e.KeyChar);
this.Text += newChar;
// set the caret to the end
this.SelectionStart = this.Text.Length;
// tell the rest of the event chain there is no further work to do
e.Handled = true;
// I'm not sure if this line is necessary. it works without it
base.OnKeyPress (new KeyPressEventArgs(newChar));
}


Jul 21 '05 #6
Z
Yep. Works good. Thanks again.

"Joshua Flanagan" wrote:
This worked for me

protected override void OnKeyPress(KeyPressEventArgs e) {
//translate your character - I use ToUpper just for an example
Char newChar = Char.ToUpper(e.KeyChar);
this.Text += newChar;
// set the caret to the end
this.SelectionStart = this.Text.Length;
// tell the rest of the event chain there is no further work to do
e.Handled = true;
// I'm not sure if this line is necessary. it works without it
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Z wrote:
Actually, on closer inspection, it does not work. I still had remnants of
code left in OnKeyDown and that was getting in the way. In OnKeyDown I can
call a method that accepts the character I want to display instead of the
character that was typed, and that method just concatenates that new char to
the existing chars already in the TextBox. Although this does work, the
cursor (as I mentioned) does not advance. But calling
base.OnKeyPress(newchar) does not render the new character. The typed
character is the one that shows up.

"Z" wrote:

Thanks, Joshua, for your suggestion. This does work as you say. One problem
remains, and that is that the cursor (or caret) does not advance past the
newly inserted character. Guess I'll now have to work on developing a custom
Caret class that I will control via code. Thanks again for your help.
"Joshua Flanagan" wrote:
The Keys required by KeyEventArgs is an enumerated type, you don't use
the constructor. For example, to simulate pressing Enter:
new KeyEventArgs(Keys.Enter)

I don't think you'll be able to do your code page translation with the
KeyEventArgs class.

Do you have to use OnKeyDown? Would OnKeyPress meet your needs?

protected override void OnKeyPress(KeyPressEventArgs e)
{
// perform your logic to get the key from another codepage
Char newChar = translateChar(e.KeyChar);
base.OnKeyPress (new KeyPressEventArgs(newChar));
}
Joshua Flanagan
http://flimflan.com/blog
Z wrote:

>I have sub-classed the TextBox. In its OnKeyDown event I can intercept key
>strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want
>to change it to another unicode key from a different code page (e.g.,
>"\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs
>are all only getters. They are read only and thus do not permit changing the
>assigned KeyCode. My question is this: How do I change the KeyCode of the
>KeyEvenetArgs to be a different key? I tired to call base.OnKeyDown() passing
>a new instance of KeyEventArgs, but the constructor for KeyEventArgs() takes
>an instance of Keys, and THAT constructor takes no argument. Even if I create
>an instance of Keys, then I'm faced with the read only nature of KeyCode,
>KeyData, etc. It seems to be a vicious circle. Any help would be appreciated.

Jul 21 '05 #7

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

Similar topics

8
by: oeyvind toft | last post by:
How do I capture the ctrl keypress? Is it possible for js to intercept the ctrl-n combination and prevent the creation of a new IE window?? (Have 3 huge books on js and none of them mention...
1
by: Matthew | last post by:
I have this code snippet that is ignoring a Tab key and executing something(); on all other key-presses. How would I efficiently modify this to include a second key, such as backspace (KeyCode 8)?...
1
by: songkau | last post by:
Why doesn't this work? :- <BODY onkeydown="display(event)"> <input type="text" name="text1"> <script language="Javascript"> function display(event) { document.text1.value = event.keyCode+' :...
17
by: Julia Briggs | last post by:
Are there any gotchas using if (event.keyCode==8)? I understand that to represent backspace, but it doesn't work. I am running Windows XP, using a typical keyboard - but no luck in detecting...
6
by: Z | last post by:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want to change it to another unicode key from a...
3
by: Pugi! | last post by:
I got this (piece of) script from 'DHTML Utopia - Modern Webdesign - Using Javascript & DOM'. function aKeyWasPressed(e) { if (window.event) { var key = window.event.keyCode; } else { var key...
5
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
3
by: Paul Gorodyansky | last post by:
Hi, I have function that works via onkeypressed - for example, what to catch when a user presses a punctuation symbol such as '.' So I check keyCode for that - works just fine in Opera 8,...
2
by: ShayHk | last post by:
hi I want to know if there is a way to change the keycode on press the main goal is to switch any languagh to english for example when I press on "א" (hebrew) the output will return as "t".....
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: 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
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: 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
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...
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...

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.