KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: Vocola 2, Mark would you please...
Topic Summary:
Created On: 12/11/2021 09:48 PM
Status: Post and Reply
Linear : Threading : Single : Branch
 Vocola 2, Mark would you please...   - BigTech - 12/11/2021 09:48 PM  
 Vocola 2, Mark would you please...   - BigTech - 12/12/2021 06:21 AM  
 Vocola 2, Mark would you please...   - BigTech - 12/13/2021 09:01 PM  
 Vocola 2, Mark would you please...   - BigTech - 12/18/2021 07:39 PM  
 Vocola 2, Mark would you please...   - alexander - 01/05/2022 09:25 PM  
 Vocola 2, Mark would you please...   - BigTech - 01/27/2022 07:29 PM  
Keyword
 12/11/2021 09:48 PM
User is offline View Users Profile Print this message


BigTech
Senior Member

Posts: 102
Joined: 11/25/2008

...make a modification to your Window.Go() so that it holds the Alt key before asking Windows to switch windows?

Please see the 2nd page of the thread I just bumped... "Bring window to front".

It goes like...

Hold Alt

If target window is minimized/iconic, activate it

else

Switch to target window

Release Alt

I don't do Python. I had a kludgy C++ method that worked but an update to one of our necessary utilities nuked it.

I will pull together all of the C++ code to reliably switch windows, as I recall it's not much. Maybe MFC helps.

Thanks.



 12/12/2021 06:21 AM
User is offline View Users Profile Print this message


BigTech
Senior Member

Posts: 102
Joined: 11/25/2008

if(GetTitle)
{//K, reading input for window switching
    if(KeyUp)
    {//K-IN, upstroke
        if((wParam==VK_OEM_5)&&(GetKeyState(VK_SHIFT)&0x8000))
        {//K-IN-UP, switch windows
            EnumWindows(EnumWinHandle,NULL);
            //FND-NRM-ACT, press alt key
            ZeroMemory(SI,sizeof(INPUT));
            SI[0].type=INPUT_KEYBOARD;
            SI[0].ki.wVk=VK_MENU;
            SI[0].ki.dwFlags=0;
            SendInput(1,SI,sizeof(INPUT));
            if(IsIconic(WndoCurrent))
            {//FND-NRM-ACT, restore destination window if minimized
                OpenIcon((WndoCurrent));
            }//FND-NRM-ACT, restore destination window if minimized
            else
            {//FND-NRM-ACT, switch to destination window
                SetForegroundWindow(WndoCurrent);
            }//FND-NRM-ACT, switch to destination window
             //FND-NRM-ACT, release alt key
            SI[0].ki.dwFlags=KEYEVENTF_KEYUP;
            SendInput(1,SI,sizeof(INPUT));
            GetTitle=false;
            *KeyString='\0';
        }//K-IN-UP, switch windows
        else
        {//K-IN-UP, add characters to titlebar search string
            if(wParam!=VK_SHIFT)
            {//K-IN-UP-ADD, printable character
                GetKeyboardState(KeyboardState);
                ToAscii(wParam,MapVirtualKey(wParam,0),KeyboardState,(LPWORD)KeyInput,0);
                strcat(KeyString,KeyInput);
            }//K-IN-UP-ADD, printable character
        }//K-IN-UP, add characters to titlebar search string
    }//K-IN, upstroke
    return 1;
}//K, reading input for window switching

I don't use proper casing for variables, and spacing is compact as the computer can read it (except for brace formatting).

Happy to help anyway I can...



 12/13/2021 09:01 PM
User is offline View Users Profile Print this message


BigTech
Senior Member

Posts: 102
Joined: 11/25/2008

[updated as needed]

 

Needless disclaimer: Don't do this stuff unless you know what you're doing.

 

I'm assuming this is okay with Mark (mdl).

 

In Mark's current window switching extension (vocola_ext_window_switching.py), replace the existing "def switch_to2(window_ID):" block with this...

 

def switch_to2(window_ID):
    if win32gui.IsIconic(window_ID):
        print win32gui.ShowWindow(window_ID, win32con.SW_RESTORE)
    else:
        print win32api.keybd_event(0x12,0,0,0)
        print win32gui.SetForegroundWindow(window_ID)
        print win32api.keybd_event(0x12,0,win32con.KEYEVENTF_KEYUP,0)
    return True

or with this...

import time #at the top with the other import statements

def switch_to2(window_ID):
    if win32gui.IsIconic(window_ID):
        print win32gui.ShowWindow(window_ID, win32con.SW_RESTORE)
    else:
        print win32api.keybd_event(0x12,0,0,0)
        time.sleep(.05)
        print win32gui.SetForegroundWindow(window_ID)
        time.sleep(.05)
        print win32api.keybd_event(0x12,0,win32con.KEYEVENTF_KEYUP,0)
    return True

 

I'm guessing bringing up the Taskbar minimized window might not have the same problem as SetForegroundWindow(). Will see. If any problems, maybe also try replacing ShowWindow() with OpenIcon() as in my C++ example since that reliably worked and it appears to be easier, FWIW, it requires only the window handle. But that is not the problem with inconsistent results. The problem is needing to hold the Alt key.

 

Looks like those "print" commands are communicating with another program or utility.

 

The timing probably is unnecessary, but I'm inexperienced with "keybd_event". If SendInput() is ever necessary, will deal with it then. SendInput() might require more code.

 

It should work every time, without exception.



 12/18/2021 07:39 PM
User is offline View Users Profile Print this message


BigTech
Senior Member

Posts: 102
Joined: 11/25/2008

OR...

 

def switch_to2(window_ID):
    print win32api.keybd_event(0x12,0,0,0)
    if win32gui.IsIconic(window_ID):
        print win32gui.ShowWindow(window_ID, win32con.SW_RESTORE)
    else:
        print win32gui.SetForegroundWindow(window_ID)
    print win32api.keybd_event(0x12,0,win32con.KEYEVENTF_KEYUP,0)
    return True

 

Seems to behave properly.



 01/05/2022 09:25 PM
User is offline View Users Profile Print this message

Author Icon
alexander
Top-Tier Member

Posts: 207
Joined: 07/31/2016

good job figuring this out. in my case I just did the following

Keys.SendInput({alt_hold}) # apparently this improves the reliability of activating Windows
Window.Go_( RegularAnd("Sublime Vocola") "((?!extension).)*$" )
Keys.SendInput({alt_release})
Wait(800)
Keys.SendInput({alt_release})


I'm not sure if I needed to send alt_release twice, but I don't think it hurts anything either and it might've made it more reliable. still your solution is probably better
 01/27/2022 07:29 PM
User is offline View Users Profile Print this message


BigTech
Senior Member

Posts: 102
Joined: 11/25/2008

My hacks might not work if by chance you are "switching" from the window you are currently in to that same window. I would include an if-then statement, testing for whether I am already in the target window, but offhand I don't know how.

But there is one quick and easy workaround!

Just add a click in the lower right-hand corner of your screen to minimize all windows. That only requires a tiny (1/10 second or less) pause for Windows to do its thing.
Statistics
32285 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 0 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 522 guests browsing this forum, which makes a total of 522 users using this forum.

FuseTalk Standard Edition v4.0 - © 1999-2022 FuseTalk™ Inc. All rights reserved.