Author Topic: How to Fire a KeyPress Event in a IE Field?  (Read 572 times)

mcjathan

  • Newbie
  • *
  • Posts: 36
How to Fire a KeyPress Event in a IE Field?
« on: June 15, 2017, 02:24:00 pm »
Greetings,

I've been updating some of our Winbatch scripts to run on the newer Windows and newer Internet Explorer (IE).  This forum thread titled "Changes to Internet Explorer DOM" has been very helpful:

     http://forum.winbatch.com/index.php?topic=923.0

However, I've run into a roadblock that I need some help with.  There is a field that I'm able to fill with the desired value.  However, to proceed to the next web-page, there is a "Next" button that must be clicked but this "Next" button is disabled until the field detects a keypress (the same field I'm loading with a value).  Simply loading the field with my desired value doesn't fire a keypress.

A screenshot of the web-page is attached at the bottom of this posting.

Here is the HTML code from that field:

Quote
<input name="question.answer" value="" id="securityAnswer" tabindex="1" class="inputm textborder" maxlength="40" onkeypress="return keyPressEvent(event)" aria-required="true" type="password">

Here's my Winbatch code that successfully fills the field with my value and attempts to click on the field so I can send keys to the field:

Quote
myinput = all.item("securityAnswer")
objEvent = browserdoc.createEvent("HTMLEvents")

myinput.value = @tmp_Array[0, @AnswerCol]   ; Enter security question's answer

objEvent.initEvent("click", @true, @true)
myinput.dispatchEvent(objEvent)

I've tried "click" and "focus" but I can't select the field (so I can send keystrokes like a left or right arrow).  An even better solution would be if I could programmatically fire a key event.

Any suggestions?

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #1 on: June 15, 2017, 07:46:38 pm »
Have you tried appending a @LF or @CRLF to your input string value?
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #2 on: June 16, 2017, 10:35:37 am »
I hadn't thought of trying to append @LF or @CRLF.  Unfortunately, however, this didn't work   :(

Right after entering the value plus the @LF or @CRLF into the field, I had my Winbatch script exit.  Then, manually, I clicked in the field and arrowed left (or right).  The "Next" button enabled instantly once I arrowed.  Then, continuing manually, I clicked on the "Next" button and everything worked properly.  So close...

Please, any other suggestions?

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #3 on: June 16, 2017, 11:07:05 am »
The @lf or @crlf was a shot in the dark so it is not surprising that it didn't work.  It could be something simple that we are just overlooking. When you state that you have tried sending a "click" event, does that mean that you have tried sending a "click" event to the myinput object or to the arrow object?
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #4 on: June 16, 2017, 01:11:42 pm »
Have you tried enabling the button?

Jim

mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #5 on: June 16, 2017, 03:29:48 pm »
The @lf or @crlf was a shot in the dark so it is not surprising that it didn't work.  It could be something simple that we are just overlooking. When you state that you have tried sending a "click" event, does that mean that you have tried sending a "click" event to the myinput object or to the arrow object?
  I've tried both clicking and focusing on the myinput object.  I'm not sure what you mean by the arrow object.

mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #6 on: June 16, 2017, 05:06:55 pm »
Have you tried enabling the button?

Jim
Hi Jim,  Thanks for jumping in!
That hadn't occurred to me. 

Here's the old code that locates and clicks on this button:

Quote
inputlist = browserdoc.GetElementsByTagName("A")
For i = 0 To inputlist.length-1
   thisinput = inputlist.item(i)
   temp2 = thisinput.id
   If temp2 == "continuebutton" 
      thisinput.click
      ObjectClose(thisinput)
      Break
   EndIf
Next

Keeping in mind that this code needs to be modernized, I would be looking to add code like this:

Quote
objEvent.initEvent("click", @true, @true)
Anchor.dispatchEvent(objEvent)

But perhaps look for a "enable" instead of a "click"?  I tried Googling for this but couldn't find anything.

Have you enabled a button like this before?

JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #7 on: June 16, 2017, 06:20:15 pm »
Let me see if I can track something down.  If you can find the keypress event source code it should have what you need since they have obviously disabled it and then enable it on a keypress.   If you do that then the plan to click should work.

Jim

JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #8 on: June 16, 2017, 06:39:58 pm »
 If the following works then just do your click after, as you intended.

Jim

Code: Winbatch
 ybrowser = your browser object...

    button = ybrowser.document.GetElementById("button_id")

    button.disabled = !button.disabled

 



mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #9 on: June 20, 2017, 04:19:30 pm »
If the following works then just do your click after, as you intended.

Jim

Code: Winbatch
 ybrowser = your browser object...

    button = ybrowser.document.GetElementById("button_id")

    button.disabled = !button.disabled

 
Unfortunately, this didn't work.

Any other suggestions?

JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #10 on: June 20, 2017, 05:58:05 pm »
The button doesn't become active?

Jim

mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #11 on: June 21, 2017, 02:16:29 pm »
The button doesn't become active?
Unfortunately, no, it doesn't become active.

Here's my actual Winbatch code:
Code: Winbatch
button = browserdoc.GetElementById("continuebutton")
button.disabled = !button.disabled
This Winbatch code executes without error, but the button simply stays disabled.

I don't know if this is helpful, but here's the HTML code for this button initially (while disabled) and after I manually caused a keyevent.  Here's the button's HTML while disabled:
Code: Text
<a class="btn btnOff " id="continuebutton" href="javascript:CallContinue();" tabindex="1"><span>Next </span></a>

Here's the button's HTML code after it's been manually enabled:
Code: Text
<a class="btn  " id="continuebutton" href="javascript:CallContinue();" tabindex="1"><span>Next
</span></a>

The button's class changes. Is that significant in regards to coming up with a solution?

Also, I see that when the button is pressed (after enabling), the javascript function "CallContinue() is executed.  Could we simple execute this function directly?

JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #12 on: June 21, 2017, 02:56:20 pm »
Yes.   The Class is changing it back and keeping it disabled.

Just change the Class to what they show.  You may still need to enable the button.  Not seeing the CSS I couldn't say either way.

Crazy busy right now so haven't tested but something along the lines of the following might do what you need.   

button.className = "btn  "
button.disabled = !button.disabled

Jim

mcjathan

  • Newbie
  • *
  • Posts: 36
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #13 on: June 21, 2017, 03:10:40 pm »
That did the trick, Jim!
Code: Winbatch
button.className = "btn  "

I needed to remove this line:
Code: Winbatch
button.disabled = !button.disabled

Thank you!

JTaylor

  • Pundit
  • *****
  • Posts: 748
    • Data & Stuff Inc.
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #14 on: June 21, 2017, 03:52:11 pm »
Excellent.   Sounds like the CSS handled both sides of it.

Jim

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: How to Fire a KeyPress Event in a IE Field?
« Reply #15 on: June 22, 2017, 06:58:11 am »
This thread is a bit embarrassing.  I created a new Web page a few days ago that uses javascript to change CSS styles of a page element to enable and disable that element.  Didn't make the connection... 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates