Send a key without focus

Just starting out? Need help? Post your questions and find answers here.
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Send a key without focus

Post by Faildeath »

Hi there,

I checked on the forum and found nothing to help me (or missed it).

I need to send a key to a program without giving it the focus so it stays in the back letting me work.
I now it's possible, I just don't know how.

I tried to look on the web and they talked about SendMessage_() so I look at it but,
yeah, I have no idea how to use this for sending a key to a windows.

So if someone can help or has another method easier (or not) as long as it works :)

Thanks !
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Send a key without focus

Post by infratec »

infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Send a key without focus

Post by infratec »

Ok,

forget my previous posting.

SendInput() works only with focus.

But I found
SendMessage(hWnd_Console, WM_SETTEXT, 0, txt)

Maybe one of our windows specialists know more about this.

Bernd
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Send a key without focus

Post by Faildeath »

I use a modified version of the same procedure, but doent work if no focus,
I also found the SendMessage() but I don't really now how to use it, with what parameters etc.

Anyway thanks for the replies :)
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Send a key without focus

Post by TI-994A »

Faildeath wrote:I also found the SendMessage() but I don't really now how to use it, with what parameters etc.
Hello Faildeath. Not sure if this is what you're looking for, but this example sends input to another window without activating its focus. I've tried it with Notepad and Calculator, and it seems to works:

Launch Notepad, and try this:

Code: Select all

hWnd = FindWindow_(0, "Untitled - Notepad")   ;window title must match
cWnd = GetWindow_(hWnd, #GW_CHILD)
SendMessage_(cWnd, #WM_SETTEXT, 0, "This is a test...")
Launch Calculator, and try this:

Code: Select all

hWnd = FindWindow_(0, "Calculator") 
cWnd = GetWindow_(hWnd, #GW_CHILD)
SendMessage_(cWnd, #WM_SETTEXT, 0, "666")
I'm sure it goes without saying that the caption in the window's title bar must match. Hope it helps.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Send a key without focus

Post by Faildeath »

Hi TI-994A, it's exactly what I wanted thanks!
But strangely the notepad one work but not the calculator one, probably the WM_SETTEXT
not recognized by the calculator.

Thanks now I'm on the good track, I'll also try to send it Key combinations & other keys (Ctrl+C / F3 etc)

Thanks for the fast help :)
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Send a key without focus

Post by TI-994A »

Hello again Faildeath; I'm so glad that it helps. Another issue to take note of is the number of gadgets the background window has, and their Z-Order. If it has more than one gadget, the example I had posted earlier would simply send the specified text to the first gadget of that window. To handle this, you should use the GetWindow() function with the #GW_HWNDNEXT flag to enumerate the gadgets, and you must also know the gadget-order for that window. This example illustrates that:

Code: Select all

EnableExplicit

Enumeration
  #MainWindow
  #BackgroundWindow
  #Gadget_1
  #Gadget_2
  #Gadget_3
  #Gadget_4
  #Button_1
  #Button_2
  #Button_3
  #Button_4
EndEnumeration

Define appQuit, wFlags = 13107201, hWnd, cWnd_1, cWnd_2, cWnd_3, cWnd_4

OpenWindow(#BackgroundWindow, 0, 0, 400, 190, "The Background Window", wFlags)
ButtonGadget(#Gadget_1, 10, 30, 100, 40, "Button")
OptionGadget(#Gadget_2, 290, 30, 100, 40, "Option")
StringGadget(#Gadget_3, 10, 100, 100, 80, "String")
EditorGadget(#Gadget_4, 290, 100, 100, 80, #PB_Text_Right)
SetGadgetText(#Gadget_4, "Editor")

OpenWindow(#MainWindow, 0, 0, 160, 220, "Front Window", wFlags)
ButtonGadget(#Button_1, 10, 35, 60, 60, "Send to 1st Child", #PB_Button_MultiLine)
ButtonGadget(#Button_2, 90, 35, 60, 60, "Send to 2nd Child", #PB_Button_MultiLine)
ButtonGadget(#Button_3, 10, 125, 60, 60, "Send to 3rd Child", #PB_Button_MultiLine)
ButtonGadget(#Button_4, 90, 125, 60, 60, "Send to 4th Child", #PB_Button_MultiLine)

hWnd = FindWindow_(0, "The Background Window")
cWnd_1 = GetWindow_(hWnd, #GW_CHILD)
cWnd_2 = GetWindow_(cWnd_1, #GW_HWNDNEXT)
cWnd_3 = GetWindow_(cWnd_2, #GW_HWNDNEXT)
cWnd_4 = GetWindow_(cWnd_3, #GW_HWNDNEXT)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_1
          SendMessage_(cWnd_1, #WM_SETTEXT, 0, "First gadget...")
        Case #Button_2
          SendMessage_(cWnd_2, #WM_SETTEXT, 0, "Second gadget...")
        Case #Button_3
          SendMessage_(cWnd_3, #WM_SETTEXT, 0, "Third gadget...")
        Case #Button_4
          SendMessage_(cWnd_4, #WM_SETTEXT, 0, "Fourth gadget...")
      EndSelect
  EndSelect
Until appQuit = 1
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Send a key without focus

Post by MachineCode »

Note that #WM_SETTEXT only inserts text at the current cursor position -- it doesn't simulate keystrokes, which is an entirely different concept. Just FYI.

Also, I did once have code that simulated actual keystrokes to an out-of-focus window, but I can't find it right now. Not sure if I have a backup somewhere (I'm looking).
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Send a key without focus

Post by Faildeath »

Thanks again you two for your help.
After a bit of diggin'I found what I was searching for.
It seems to be workin now, I'll still play a bit ;ore with it to be sure it's all good but that should do it :

Code: Select all

SendMessage_(hWnd, #WM_KEYDOWN , #VK_NUMPAD2, 29)
Strangely this works on my program but does not with Calc nor with Notepad.
Well, anyway, as long as I can make it works it's good for me :)
Post Reply