kutz Posted March 2, 2005 Posted March 2, 2005 Greetings, As indicated in the title I am trying to work out how to raise a keypress event in code, something I cannot find an article or tutorial on anywhere!! Which, to me, would be counter-inuitive as it would seem to be incredibly simple. Any help would be greatly appreciated. Regards, Kutz Quote
Leaders snarfblam Posted March 2, 2005 Leaders Posted March 2, 2005 Are you trying to raise an event in your own control? Are you trying to cause another control to raise a keypress event? Are you just trying to call a keypress handler function? What exactly are you trying to do? Quote [sIGPIC]e[/sIGPIC]
twistedm1nd Posted March 2, 2005 Posted March 2, 2005 //C# code // KeyPressEventArgs accepts a char as the input private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // do something based on the key pressed } private void button1_Click(object sender, System.EventArgs e) { char ch = 'a'; Form1_KeyPress(new object(),new System.Windows.Forms.KeyPressEventArgs(ch) ); } OR the other way would be to use keybd_event winapi Quote
kutz Posted March 2, 2005 Author Posted March 2, 2005 Cheers gents for your replies so far. I'm fine with standard keypress event handling. What I am trying to do is simulate a key being pressed within a particular control. (In this case, a DateTimePicker) So the idea being that without the user touching a key, the program acts as if a user hit the 'enter' key while that control is focussed. The trigger for this is clicking a button on the form. Thanks for your help. Quote
pas30339 Posted March 4, 2005 Posted March 4, 2005 (edited) This will fire/raise the KeyPressEvent simulating the Enter key being pressed. private void FakeEnterKeyPress() { KeyPressEventArgs e= new KeyPressEventArgs(Convert.ToChar(13)); OnKeyPress(e); } Edited March 4, 2005 by pas30339 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.