3

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.

cottontail
  • 10,268
  • 18
  • 50
  • 51
Brent Henson
  • 33
  • 1
  • 1
  • 6

2 Answers2

4

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..

barlop
  • 12,887
  • 8
  • 80
  • 109
  • That isn't what I was asking for. The comment that you are replying to explains how to use the number pad which is what I needed to complete the task I was working on. Thank you for your efforts though; but you can see in my question I mention using the SendKeys method already with a basic string. – Brent Henson Sep 17 '10 at 17:12
  • His answer, which was objwsh.SendKeys chr(79) & chr(80) & chr(81) <--- That displays OPQ. It doesn't display numbers. I tested it with Set objShell = CreateObject("WScript.Shell") and with objShell.SendKeys chr(79) & chr(80) & chr(81) So I don't understand how it helped 'cos you wanted something involving the number pad. – barlop Sep 19 '10 at 12:54
  • 1
    Oh I see why you are confused. The help was the example and the link to the page. I was able to use it to complete what I was trying to do. Either way I appreciate both your efforts! – Brent Henson Feb 13 '11 at 02:06
  • @BrentHenson so what line did you / would you have used then? – barlop Jan 04 '19 at 19:19
  • Sorry for the slow response, very rarely do I log into stackoverflow. I don't recall what I used, it was 11+ years ago, have a good one :) – Brent Henson Nov 28 '19 at 18:32
  • @BrentHenson Also you accepted a solution that only helped you towards a solution so you found the solution and kept it hidden from people. And then you accepted a solution that isn't technically a solution, which is not good for the database of questions and answers. So not only did you hide your solution, but you made the database of QnA poorer by marking a non-answer as an answer. – barlop Nov 30 '19 at 00:24
  • @BrentHenson this site is mainly about helping any people that see a question, not just about helping yourself / the questioner. – barlop Nov 30 '19 at 00:27
3

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) 
unrealtrip
  • 670
  • 4
  • 13