Jump to content

Using "Send" to Send Keystroke to "Most Recently Used" Window


Recommended Posts

I've been using AutoIT for some time now... I've found it to be a VERY handy tool and have used it for countless "automation" projects... I'm now trying to write what I thoguht would be a simple script to insert a DATE/TIME stamp into the "currently active window"... Often I might be working in WORD or EXCEL or in a TEXT file and I like to insert the current date/time in a "custom format". I simply want AUTOIT to SEND the DATE/TIME string I've created to "whatever application window was active" before the script was executed.

For my initial testing... I created a "desktop icon shortcut" pointing to the script... However, if - for example - I was in MS WORD and double-clicked the shortcut, nothing happened. Meaning that the DATE/TIME text string did not appear anywhere. Then, I added a MSBGOX line to the script and... Voila... Once you click OK on the MSGBOX the text is then successfully typed into the MS WORD document.

I'd prefer NOT to have to do the MSGBOX step - but there is obviously something I'm missing here... Can anyone suggest a way to make this work... Any help you can provide would be greatly appreciated.

CODE
#include <Date.au3>

$datetimestring = _NowDate() & " at " & _NowTime()

MsgBox(1, "Date Time String", $datetimestring)

Send($datetimestring) ; SAMPLE: 10/23/2006 at 8:08:02 PM

Link to comment
Share on other sites

When you click on the script on the desktop, it takes focus away from MS Word or whatever the program is that you're working in, so the text is sent directly to the desktop.

Try using:

#include <date.au3>
Opt("WinTitleMatchMode",2)
WinActivate("Microsoft Word");replace that with whatever the program is.

$datetimestring = _NowDate() & " at " & _NowTime()

;MsgBox(1, "Date Time String", $datetimestring)

Send($datetimestring) ; SAMPLE: 10/23/2006 at 8:08:02 PM

Infinity is a floorless room without walls or ceiling.Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable subhuman who has learned to wear shoes, bathe, and not make messes in the house.

Link to comment
Share on other sites

When you click on the script on the desktop, it takes focus away from MS Word or whatever the program is that you're working in, so the text is sent directly to the desktop...

You are correct, but I think that the OP wants to be able to send the info to any currently active window. MS Word was just an example. The OP will not always know the title for the window of interest.

@UmmDoughnuts,

Take out the MsgBox line

Save your script as an au3 file (or as a complied script if you like)

Place a shortcut on your desktop* to that script file

Assign** the shortcut a "Shortcut key" like ctrl-alt-d

Now you can launch your script without the current window (whatever it is) losing focus.

*If you do not want the shortcut on your desktop, you can place it somewhere in the Start > Programs folder structure, but you may have to restart your system for any OS "Shortcut key" to take effect.

**Not to insult you here, but just in case you (or others) don't know what I'm talking about - it is the operating system (OS) shortcut key. Right-click on the OS shortcut and select "Properties"... then in the filed named "Shortcut key" - type the letter d.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

The best way to do it is to use a hotsetkey. I set mine to work on the F9 key, and only when certain windows are active. Works very well. This way you can have the window you want to be active, and not lose focus when you stamp it.

Func _TimeStamp(); TimeStamp
    $sLongDayName = _DateDayOfWeek(@WDAY)
    $stamp = _NowTime()
    $stamp2 = _NowDate()
    If WinActive("Service Request", "equest.nsf") Then
        Send("---------------------------")
        Send("{ENTER}")
    EndIf
    Sleep(20)
    Send(@UserName & ": " & $sLongDayName & " " & $stamp2 & ", " & $stamp & ": ")
    ;EndIf
EndFunc   ;==>_F9A_Func

What it liiks like when I use the time stamp:

---------------------------

user: Wednesday 01/03/2007, 2:18:12 PM:

Link to comment
Share on other sites

THANKS everyone... I appreciate all of the replies... I actually used a "combination" of the suggestions and things are working great now. Oh... And PLEASE don't worrying about "insulting my intelligence"... The best stuff is usually the simple stuff... I've been using Windows for more years than I care to mention and I had never heard of / used the "OS Shortcut Key" setting to run a program...

Just for my future reference... Is there a way to tell what window was actually "active" at the time an AUTOIT script is executed... ? I know how to activate windows and I understand window handles work but I wasn't sure if there was any kind of "stack" that Windows maintained that could be referenced to find the "previously active" window ?

THANKS again !

Link to comment
Share on other sites

...Just for my future reference... Is there a way to tell what window was actually "active" at the time an AUTOIT script is executed... ? I know how to activate windows and I understand window handles work but I wasn't sure if there was any kind of "stack" that Windows maintained that could be referenced to find the "previously active" window ?...

I would not know how to get at such a stack even if it did exist.

If you run a script all of the time and use a hotkey to trigger the sending of you data, then that script can track window title changes. Run this little sample:

While 1
    $WinTitle1 = WinGetTitle("")
    Sleep(99)
    $WinTitle2 = WinGetTitle("")
    If $WinTitle1 <> $WinTitle2 Then
        $temp = $WinTitle2
        TrayTip("", "Old:  " & $WinTitle1 & @CR & "New:  " & $temp, 100, 16)
    EndIf
WEnd
then click on some windows.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Sleep(99) isn't going to produce a long enough time to do anything. That is less than a hundredth of a second.

Sleep(9) works too well :-) so, I took it up to Sleep(99).

Here is what I mean by too well:

Run that little sample code using Sleep(9) from within SciTE

Click on an empty place on your desktop

(You should see the TrayTip pop up listing the current and last window title.)

Click on the rectangle associated with "SciTE" on the Windows Taskbar

Click on another rectangle on the Windows Taskbar

[but hold the click down long enough to see that the script catches the blank window title of the Windows Taskbar]

In an effort to not bother tracking the taskbar, I increased the Sleep time to 99. I thought about keeping the Sleep(9) and not passing the window title to $temp if it is blank... but this was meant to be simple/sample code to demonstrate one method of doing what the OP requested.

You could use a higher Sleep time, just not too high or you risk losing track of some windows.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...