Changing a key (OnKeyPress) and cancelling a TextChanged while retaining the cursor position

Z

Zief

Hi,

I am trying to figure out how to do a few things with key events in my
own class extending the TextBox.
I would like to change a ',' character into a '.' character in the
OnKeyPressed event. The KeyChar value is read-only though. I have
searched and found a post where it says to use PostMessage to try and
post a '.' character back to the control, and then use e.Handled = true
to cancel the ',' character, but this does not seem to work. Does
anyone have any suggestions?

I would also like to cancel certain key presses, but this cannot be
determined in the OnKeyPressed event, as you cannot analyse the text
with the key that has been pressed (in the particular position it is
going to go into the text) at this point. The Text property only holds
the previous text in this event. I can determine whether or not to
cancel the key in the TextChanged event, but if I then revert this to
the previous text, the cursor position reverts to the beginning of the
textbox from whatever position it was previously in.
Again after a search, I thought I would be able to use GetCaretPos
before the change to save the cursor position, and the SetCaretPos
after the change to return it to the correct place. This also seems to
have no effect, so I am looking for suggestions here as well.

Thanks in advance of help,
Zief
 
A

ackesa

Hello, I'm using the following code to chatch Every keyevent. Maybe you
could modify the Winproc-Method to send a '.' instead of a','.

Regards,
Sabine

public class KeyProcessor : MessageWindow
{
[DllImport("CEKeyBoardHook.dll", EntryPoint="Initialize")]
private static extern void Initialize( IntPtr form );

[DllImport("CEKeyBoardHook.dll", EntryPoint = "Install")]
private static extern int Install( int code );

[DllImport("CEKeyBoardHook.dll", EntryPoint = "UnInstall")]
private static extern int UnInstall();

private struct KeyboardHookData
{
public int vkCode;
public int scanCod;
public int flags;
public int time;
public uint extraInfo;
}

private const int WM_USER_KEYMESSAGE = 0x400;
public const int VK_F1 = 0x70;
public const int VK_F2 = 0x71;
public const int VK_F3 = 0x72;
public const int VK_F4 = 0x73;

public const int VK_F5 = 0x74;
public const int VK_F6 = 0x75;
public const int VK_F7 = 0x76;
public const int VK_F8 = 0x77;

public const int VK_F9 = 0x78;
public const int VK_F10 = 0x79;
public const int VK_F11 = 0x7a;
public const int VK_F12 = 0x7b;

public delegate void KeyboardCallback( int keyCode );
public event KeyboardCallback OnKeyboard;

private int m_oldCode = -1;

private void FireOnKeyboard( int keyCode )
{
if( OnKeyboard != null )
{
if (m_oldCode == keyCode)
{
OnKeyboard(keyCode);
m_oldCode = -1;
}
else
{
m_oldCode = keyCode;
}
}
}

public KeyProcessor()
{
KeyProcessor.Initialize( this.Hwnd );

int retCode = KeyProcessor.Install( 20 );
if( retCode != 0 )
{
MessageBox.Show( retCode.ToString(), "Fehler" );
}
}

~KeyProcessor()
{

KeyProcessor.UnInstall();
}


protected override void WndProc( ref Message msg )
{
base.WndProc(ref msg);
switch (msg.Msg)
{
case WM_USER_KEYMESSAGE:
{
KeyProcessor.KeyboardHookData hookData;

hookData =
(KeyProcessor.KeyboardHookData)System.Runtime.InteropServices.Marshal.PtrToStructure(msg.LParam,
typeof(KeyProcessor.KeyboardHookData));
this.FireOnKeyboard(hookData.vkCode);
break;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top