PC Review


Reply
Thread Tools Rate Thread

Changing KeyCode in OnKeyDown

 
 
=?Utf-8?B?Wg==?=
Guest
Posts: n/a
 
      10th Mar 2005
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.
 
Reply With Quote
 
 
 
 
Joshua Flanagan
Guest
Posts: n/a
 
      10th Mar 2005
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.

 
Reply With Quote
 
=?Utf-8?B?Wg==?=
Guest
Posts: n/a
 
      10th Mar 2005
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.

>

 
Reply With Quote
 
=?Utf-8?B?Wg==?=
Guest
Posts: n/a
 
      10th Mar 2005
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.

> >

 
Reply With Quote
 
Joshua Flanagan
Guest
Posts: n/a
 
      11th Mar 2005
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.
>>>

 
Reply With Quote
 
=?Utf-8?B?Wg==?=
Guest
Posts: n/a
 
      11th Mar 2005
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));
> }


 
Reply With Quote
 
=?Utf-8?B?Wg==?=
Guest
Posts: n/a
 
      11th Mar 2005
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.
> >>>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the KeyCode on XP JCO Windows XP Customization 14 20th Nov 2006 11:15 PM
"Keycode DLL not found or invalid keycode" Marvin Massih Microsoft ASP .NET 0 31st Dec 2004 05:40 PM
problem with OnKeyDown news.microsoft.com Microsoft C# .NET 3 19th Oct 2004 02:03 PM
OnKeyDown Jaga Microsoft Dot NET Framework Forms 3 19th Feb 2004 01:50 AM
OnKeyDown - two events? Drew Microsoft Dot NET Compact Framework 3 26th Nov 2003 11:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:56 PM.