1

I have made some code that only appears ontop of OneNote and offers some buttons. The problem I have with it is that when I press the button it gets focus so the GUI disappears. I have found two possible solutions, but I'm not sure how to implement them.

My first idea would be to make the GUI unfocusable, but as stated I don't how that would work.

The second thought is that I could make

    WinWaitNotActive  ‎- OneNote

Only pass when neither the OneNote nor the GUI are active, but I also don't know how to pass two possible programs into the command.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#Notrayicon

    SetTitleMatchMode 2

    Gui +LastFound -Caption +ToolWindow +Border + AlwaysOnTop
    Gui Add, Button, gDublicate x-1 y-1 w60 h25, Dublicate
    Gui Add, Button, gDelete x57 y-1 w60 h25, Delete
    Gui Add, Button, gBackwards x115 y-1 w25 h25, ←
    Gui Add, Button, gForwards x138 y-1 w25 h25, →

    Loop{
        WinWaitActive  ‎- OneNote
        Gui, Show, xCenter y35 NoActivate h23 w162
        WinWaitNotActive  ‎- OneNote
        Gui Hide
    }

    return:

    Dublicate:
        WinActivate  ‎- OneNote
        SendInput ^c
        Sleep 50
        SendInput ^v
    return

    Delete:
        WinActivate  ‎- OneNote   
        SendInput {Del}
    return

    Backwards:
        WinActivate  ‎- OneNote   
        SendInput ^z
    return

    Forwards:
        WinActivate  ‎- OneNote   
        SendInput ^y
    return
  • what's the use case? private information on the page? high/contrast distracting UI if you are multi-tasking? – Kermit May 01 '18 at 16:51
  • @HashRocketSyntax I am using a drawing tablet for noteTaking so I often draw with the pen. Features like delete and dublicate are only possible with keyboard though. – CodeSharpMarvin May 01 '18 at 17:06
  • Since you activate OneNote in the button g-labels, doesn't `WinWaitActive ‎- OneNote` return and thus the GUI shows up again? – Josh Brobst May 01 '18 at 19:57
  • @JoshBrobst Indeed, but if I press the button too long is doesnt register after reappearing, and it also takes time so double clicking doesn't work. – CodeSharpMarvin May 01 '18 at 21:25

1 Answers1

0

For your first idea, I think you can just use the extended window style WS_EX_NOACTIVATE. It can be applied to your GUI by adding +E0x08000000 to the options:

Gui +LastFound -Caption +ToolWindow +Border +AlwaysOnTop +E0x08000000

Alternatively, I explored your second idea too. You should be able to do it using ahk_group, like so:

GroupAdd OneNote, - OneNote
; Requires adding +HwndGuiHwnd to your GUI options
GroupAdd OneNote, ahk_id %GuiHwnd%

Loop{
    WinWaitActive ahk_group OneNote
    Gui, Show, xCenter y35 NoActivate h23 w162
    WinWaitNotActive ahk_group OneNote
    Gui Hide
}

I don't have OneNote and can't truly test these, but I believe either of them should work. You might, however, run into some inconsistency since WinWait[Not]Active works by continuously checking the foreground window. This involves calling GetForegroundWindow, which may return NULL in a small time frame between switching windows, resulting in WinWait[Not]Active returning unexpectedly. In this case you might want to abandon the WinWaitActive+WinWaitNotActive loop altogether in favor of a shell hook, which I wrote about here.

Josh Brobst
  • 1,772
  • 10
  • 16