Solved

Sending keystroke from Delphi to another app

Posted on 1998-03-28
13
397 Views
Last Modified: 2013-12-03
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
Comment
Question by:DARRAH
  • 7
  • 6
13 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:DARRAH
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now