Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simulating keypress 2

Status
Not open for further replies.

CentauriAudio

Programmer
Apr 4, 2004
7
AU
Can anyone enlighten me as to how to simulate a keypress (in my case, the Enter key) in whichever object (memo box) has current focus?

I have a popup form with DBGrid, and when the user selects a record, focus passes back to the main form and I want to simulate pressing the enter key, which will activate the OnKeyPress event in the memo box.

Suggestion?

Thanks
Graeme
 
Not sure I'm getting your meaning right, but:

1) Get the focused control using Form.ActiveControl if you don't know which control it is.

2) Call the control OnKeypress event or, if direct calling don't fit your needs, send a WM_KEYDOWN message to it.

buho (A).
 
Thanks for the Form.ActiveControl tip - useful!

I found (on another forum..) suggestion of using keybd_event function, and this works a treat.

Cheers
 

xxx_events are useful but system wide if memory serves.

Your event can end managed for any application which suddenly got the focus and confuse the user (where this key command is coming from?), that is what I propossed a local event.

buho (A).
 
i get some idea without pratice from MSDN:
1. winapi function: sendinput
insert input to input stream, it's system wide;
2. postmessage WM_KEYDOWN to the thread which attch your window
i don't know whether they are right, u can try your self

way to gold shines more than gold
 


sendinput is system wide too.

To send a key/click to a control, post/send the message to the control handle if you can get it.

Keep local events local. Encapsulation is not only about code and data, it is about messages too.

Furthermore, messages sent by PostThreadMessage cant be processed by DispatchMessage (the message window handle is null), so it is limited in some way (check the SDK for more info).

buho (A).


 
Point taken re: using local events, buho.

I used Form.ActiveControl.Perform(WM_KEYDOWN,..

works well.

Thanks again
Graeme
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top