Forum Discussion

dbattaglia's avatar
dbattaglia
Contributor
9 years ago

Create a KeyEventArgs Object

We use a custom function to set text in textboxes and in the function we verify that the value was set. For known cases--like dates, uppercase text, etc--we are anticipating the formatting to happen and so we expect that the textbox.text value will be formatted.

 

In most situations the formatting is triggered in either the OnLostFocus or OnLeave events. So in our function we call the OnLostFocus or OnLeave methods on the textbox in order to trigger the formatting code. I have a situation where string formatting for a textbox is triggered via the KeyDown event of the textbox only if the [Enter] key is pressed.

 

So my first thought is to try to simulate sending the [Enter] key to the OnKeyDown method of the textbox in order to simulate this and trigger the formatting code in the app. But after reading up on the KeyEventArgs class, I have not figured out how to create a 'key' object that represents the [Enter] key.

 

Has anyone found a workable way to create a 'key' object that could be used in scripts for a situation like this?

 

details:

TestComplete 12.0.122

VBScript project

AUT is a WinForms VB.NET app using Telerik controls

 

Thanks,

Daniel

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Not EXACTLY the answer to your question... but is there a reason why you can't just call "Keys([Enter])" on the control to send the enter command without having to directly access the event handler?

    • dbattaglia's avatar
      dbattaglia
      Contributor

      I can if I have to, but I was trying to follow the pattern we have established in the wrapper function.

       

      Actually, if what I am asking about is not possible--due to language or technical limitations or what have you--then I will have to just code this script as a one-off. But that is what I am trying to avoid.

       

      That said, this form is an oddity on its own, so perhaps the one-off wouldn't be so bad.

       

      It would not be the worst thing, but I always try to maintain the patterns we have established.

       

      Regardless, I am always looking for new and interesting things to learn.

       

      Let me know if you have any suggestions.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        A few years ago, I had to simulate a credit-card swipe using a magnetic swipe reader that was "wedged" between the keyboard and the PC.  Basically, what the hardware did was convert the code in the magnetic swipe into keyboard events.  The "Keys" method of TestComplete wasn't fast enough to do this so I had to build my own code to do this rapid swipe...

         

        ...unfortunately, that was about 14 years ago at a different company with a different code language... so I don't have it at my finger tips.  The technology exists to do so but, my question that I would ask myself is, is it worth it to do something one way to "stick with a pattern" or do it an "easier" way to get the job done.  This is not a question I can answer for you, but it's something to consider.

        If I have time, I'll look around and see if I can rustle up that code.  In the meantime, you can try using LLPlayer.KeyDown and LLPlayer.KeyUp... now, this is not VBScript code, but perhaps you can adapt it:

        LLPlayer.KeyDown(VK_RETURN, 10)
        LLPlayer.KeyUp(VK_RETURN, 10)

        Another possible option - If you have the Win32API plug in installed, you basically have access to all options in that API, including keybd_event (see https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx). 

        So... other than using the Keys method, there are some potential options as well...