How do I use WScript.Shell SendKeys to send Number

2019-07-03 05:27发布

问题:

I am trying to use WScript.Shell SendKeys method to emulate sending a key press from the Number Pad.

I have an application that I am writing automated testing for using QTP. It is a Web Browser based application and the input is into a Java App within the web page. The input only accepts key presses from the Number Pad and the Enter key.

So far I am using this code:

Dim strInputKey
strInputKey = "{ENTER}"
Set objWsh = CreateObject("WScript.Shell")
Browser("Launch Browser").Page("Test Application").WebElement("Item ID").Click
objWsh.SendKeys strInputKey

This works fine for sending the Enter key, but I can't quite figure out if there is a way to send Number Keys. Any help would be greatly appreciated.

I am not sure if there are any undocumented ways of achieving this. I have read http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx but it doesn't go into great detail.

Thanks in advance,

  • Brent

回答1:

You'll need to use the keycodes for the number pad.

Here's a list of them: http://www.empirisoft.com/directrt/help/_helpcontents.htm?directrt_key_codes.htm

So to send "123", you would need to do:

objWsh.SendKeys chr(79) & chr(80) & chr(81) 


回答2:

I don't have the rep to comment on the above answer that said objWsh.SendKeys chr(79) & chr(80) & chr(81)

but I don't think it's correct

objWsh.SendKeys chr(79) & chr(80) & chr(81)
For a start, it sends the letters O,P,Q And he wants numbers, like 1234567890

and the link goes to keyboard scan codes.. I think those are for knowing what key on the keyboard was pressed. They are different from ascii codes.
79,80,81 are the keyboard scan codes for some numbers on the number pad / numpad.

Chr though, uses ascii codes. not keyboard scan codes.

Furthermore, just specifying a digit, here, since it isn't done by pressing a key, it doesn't specify and needn't specify, which was key was used, since a key wasn't used.

To sendkeys some numbers (from the number pad), is just same as sending keys from the top row. You just want to send some numbers.

If all he wants to know is how to use sendkeys to send digits, then obviously.

objWsh.SendKeys 12345 or str="12345" objWsh.SendKeys str

But if the questioner didn't realise that objWsh.SendKeys 12345 would do it, then perhaps the questioner is just confused. I guess from the green tick, he voted an answer that is like objWsh.SendKeys "OPQ".

I am aware that this is an old question, but for the sake of haing correct questions and answers..