• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

Sending keystroke from Delphi to another app

Generally, I'm using a Delphi app to update some files on remote computers. I need to be able to close out several applications that may be open. I'm able to identify if the apps in question are open, and close them, but several require additional effort. Namely, one always pops up a "Do you want to save your changes" button, which I need to respond to. The second is an app that also requires having a button clicked (or sending Alt+O).

My problem concerns passing a keyboard command (in my case, Alt+O) to the button in the other application. I can, I think, I identify the handle of the window I want to pass this to, but I'm just not getting the code right, because it isn't responding.  I have a feeling it has to do with using WM_SYSCHAR OR WM_SYSKEYDOWN but like I said before, I'm just not getting the code exact enough for it to work. Unfortunately, no examples I've seen have been specific enough to use.

Could someone give me a specific example. the previous question here does not provide me an answer because I've been down those roads, but apparently I'm doing something slightly wrong.
0
DARRAH
Asked:
DARRAH
  • 7
  • 6
1 Solution
 
nietodCommented:
answer coming.
0
 
nietodCommented:
You probably should just send the WM_SYSCHAR message.  If that doesn;t work, you may need to try to send the WM_SYSKEYDOWN and WM_SYSKEYUP messages instead.  But most likely that is not necessary.

Try

SendMessage(WndHnd,WM_SYSCHAR,'O',0x20000000);


0
 
DARRAHAuthor Commented:
Please clarify: SendMessage requires a wParam, lParam which in Delphi is an integer value. For the O value, I can send the ASCII value of 79, (I think), but for the 0x20000000, I don't know what value to give to it. What does it stand for? And is there a value I can replace it with that will work in Delphi?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
DARRAHAuthor Commented:
Please clarify: SendMessage requires a wParam, lParam which in Delphi is an integer value. For the O value, I can send the ASCII value of 79, (I think), but for the 0x20000000, I don't know what value to give to it. What does it stand for? And is there a value I can replace it with that will work in Delphi?
0
 
nietodCommented:
Yes, pass 79 for the 'O' and 536870912 for the lparam.  (That is 20000000 hex converted to decimal.)  If you don't wnat to hard-code the 79, you can convert a leter to an integer value in pascal.  I just don't rember how.  
0
 
DARRAHAuthor Commented:
I must be doing something wrong. On this particular dialog box, I have two TEdit fields to which I am able to send text. I'm getting their window handles and sending it using SENDMESSAGE(hwnd,WM_CHAR,wparam,lparam). That works. Now, I'm getting the Parent window handle, and I'm getting the TButton handle, and I know I have the two Tedit handles. Yet when I try SENDMESSAGE(HWnd,WM_SYSCHAR,79,536870912), with HWnd being any of the window handles I know, I'm getting no action. Actually, I'm getting a beep as if it doesn't recognize Alt+O as a valid keystroke. However, I know pressing Alt+O in that dialog pushes the button. I've tried sending it from each of the known window handles, and I've tried sending WM_SYSKEYDOWN followed by WM_SYSKEYUP from each of the known window handles. I'm truly stumped. I hope you have the patience and drive to give it another go.
0
 
nietodCommented:
You are trying the right things (different widnwo handles, different messages etc.)  There is, unfortunately, not much more that I can suggest.  

Do you have SPY++ or a similar tool?  You can use it to examine the messages that occur when you actually type an alt-o.  You can see what window gets the message and what the parameters are sent.  (For example, I left the OEM scan code as zero in the pararmeters because I did not know it and it really should not be needed, but perhaps it is.)
0
 
nietodCommented:
I was fooling with spy and an app that takes alt-o to open a file from a dialog.

Unfortunately I can not copy the information from spy, so I'll type in the highlights.

when the alt key is pressed you get a SYSKEYDOWN wparam = 12h lparam = 68380001h.
when the o is pressed (with alt) you get a SYSKEYDOWN wparam = 4Fh lparam = 28180001h.
then you get a SYSCHAR wparam = 6Fh lparam= 28180001h.

there are no key-up messages that I can see (there are several 100 messages)  I suspect the dialog window is destroyed before the key upmessages are received.  So they must not matter (in this case).  This is an MFC application, not a OWL or VC.  But the key-up and key down messages will be the same, the messages in response will differ.  However, it would be pest if you could test on the actual application to see exactly which window gets the messages and what it does in response.



0
 
DARRAHAuthor Commented:
Using Winsight, this is what I get when I'm in the dialog and pressing Alt+O

* WinSight message trace Mon Mar 30 1998 05:27:53 *
03693.459 001657:00000798 "Edit1"  WM_SYSKEYDOWN (10404X) Dispatched   wp=00000012  lp=21380001 12h  18d  VK_MENU   Scan 38h  Alt  Extended  Down
03693.612 001658:00000798 "Edit1"  WM_SYSKEYDOWN (10404X) Dispatched   wp=0000004F  lp=20180001 4Fh  79d  VK_O   Scan 18h  Alt  Down
03693.612 001659:00000798 "Edit1"  WM_SYSCHAR (10604X) Dispatched   wp=0000004F  lp=20180001 4Fh  79d  'O'   Scan 18h  Alt  Down
03693.859 001660:000007A8 "OK"  WM_SYSKEYUP (10504X) Dispatched   wp=0000004F  lp=E0180001 4Fh  79d  VK_O   Scan 18h  Alt  Up

Edit1 is just the value of the control that I'm in when I pressed Alt+O.

My problem is converting the values I see here to a longint (or whatever value) that Sendmessage (or Delphi?) will use. As you can see, I'm close, but still lost.
0
 
nietodCommented:
Are you saying you don't know how to convert the Hex values to decimal?  Shame on you.  Weren't you paying attention in 8th grade...Using a Hex->decimal programmer's calculator I get.

12h = 18
21380001h = 55731145
4Fh = 79
20180001h = 538443777

0
 
DARRAHAuthor Commented:
Thanks for all your help and patience in pushing me through to the answer. And, by the way, I did sleep a lot in high school.
0
 
nietodCommented:
So it is working?  If the problem was just hex-dec stuff, I recommend you invest in a programmer's calculator.  They are usually under $10 and are just the right size to fit in you shirt pocket with your pens and rullers. :- )
0
 
DARRAHAuthor Commented:
Actually, I had a DOS version of Norton Utilities that had a hex converter in it. And, I don't do pocket pals. Thanks, again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now