#๐Ÿ”’ Allowing user to type in a text field while keeping the app unfocused. (Windows user32.dll API)

108 messages ยท Page 1 of 1 (latest)

mighty arch
#
GWL_EXSTYLE = -20
WS_EX_NOACTIVATE = 0x08000000

def set_window_no_activate(handle):
    # Get the current extended window style
    current_style = ctypes.windll.user32.GetWindowLongW(handle, GWL_EXSTYLE)

    # Add WS_EX_NOACTIVATE to the style
    new_style = current_style | WS_EX_NOACTIVATE

    # Set the modified style
    ctypes.windll.user32.SetWindowLongW(handle, GWL_EXSTYLE, new_style)

Here are some things I have noticed.

  1. When I click on the application as intended the application does not get focused.
  2. My clicks still register and behave as intended and espected.
  3. When I click a textfield and start to type in it no text appears in the textfield.
  4. However if I click on the application icon in my taskbar the application gets focused.
  5. When I have the app focused by clicking on the application in the taskbar then typing in the textfields work.

Points 1, 2 and 4 are not a problem for me, but points 3 and 5 are. It is pretty frustrating that I cannot type in the textfields when the app is unfocusable.
Is there a way to allow the clicked text field to be typed in while keeping the app not focused?

ivory juncoBOT
#

@mighty arch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mighty arch
#

Example of the app when the application icon has been clicked in the windows taskbar below to "force" a focus when the app is still not focusable. Result: I am able to press all buttons and all the app components work. I can also type inside the text fields. Which works as intended. ๐Ÿ‘

#

Example of the app when the application is not focused while the app is unfocusable. Result: I am able to press all buttons and all the app components work. The app remains not focused which is great! I can click buttons and have all the behaviours work exactly the same as when the application is focused. Except for textboxes. I can click a text field and see the the bar blink indicating that the text field has focus while the app is still not focused. But when I start typing nothing shows sadly.

#

is there a way to fix this problem?

#

Update:
I did some debugging and thought maybe i can use javascript to detect the keypresses in the window when its unfocused and then appending that to the textfield since my application uses the following libraries for the UI:

  • eel
  • pywebview
    Results are as expected that the debug console of the webview window can only detect keypresses when the window is fully focused otherwise it cannot detect keypresses. However button clicks, selections, scrolling and other functionalities do work as normal...

javascript code used to test this:

function logKeyPressed(event) {
            const keyCode = event.keyCode || event.which;
            console.log(`Key pressed: ${String.fromCharCode(keyCode)}`);
        }
document.addEventListener('keydown', logKeyPressed);
sly rover
#

But like why w32?

kindred nacelle
#

how python code python?

mighty arch
sly rover
mighty arch
#

for?

#

i dont have an event listener

#

if you look up there is the function i use to make the window unfocusable

sly rover
kindred nacelle
#

python guys how to

sly rover
kindred nacelle
#

fun python not

sly rover
#

With nonsense

rapid bough
mighty arch
#

me simply changing the application windows property to unfocusable has for some reason a nice extra feature that allows me to use my mouse while keeping the app not focused

#

so the app stays not focused when i press a button

#

but in my app the button still works

#

which im glad works

mighty arch
#

i didnt need to do anything for this

sly rover
#

So it's an app that you didn't code? And you're just modifying it with python?

mighty arch
#

no its my own app

#

let me show you the code give me a sec

sly rover
mighty arch
#

no need to apologize!

#

trust me i learned a lot and a few weeks ago i would have asked the same or similar questions if someone asked me for help on this

#

๐Ÿ˜‚

#

give me a sec im copy pasting the code to show how the logic etc works

#
import threading
import keyboard
import webview
import ctypes
from ctypes import wintypes
import eel
from time import sleep

GWL_EXSTYLE = -20
WS_EX_NOACTIVATE = 0x08000000
user32 = ctypes.windll.user32

def set_window_no_activate(handle):
    # Get the current extended window style
    current_style = user32.GetWindowLongW(handle, GWL_EXSTYLE)

    # Add WS_EX_NOACTIVATE to the style
    new_style = current_style | WS_EX_NOACTIVATE

    # Set the modified style
    user32.SetWindowLongW(handle, GWL_EXSTYLE, new_style)

@eel.expose
def start():
    """Once the app starts"""
    sleep(1)
    handle = user32.FindWindowW(None, "ValKit")
    set_window_no_activate(handle)
    print("Finished window changes")

def eel_setup():
    eel.init('web')
    eel.start('index.html', port=8900, mode=None, shutdown_delay=0.0)

def create_webview():
    return webview.create_window(
        title="ValKit",
        url="http://localhost:8900/index.html",
        vibrancy=True,
        on_top=True,
        frameless=True
    )

def cleanup():
    """Gracefully exit all used resources"""
    keyboard.unhook_all()
    print("Cleaning up resources before exit")

if __name__ == '__main__':
    try:
        eel_thread = threading.Thread(target=eel_setup)
        eel_thread.daemon = True
        eel_thread.start()

        window = create_webview()
        webview.start(debug=True)
    except KeyboardInterrupt:
        print('Interrupted')
    except ValueError as e:
        print(f'ValueError: {e}')
    except Exception as e:
        print(f'An error occurred: {e}')
    finally:
        cleanup()
        os._exit(0)
#

@sly rover

#

this function gets called by the website once the application is launched (this function works) so that i dont need a infinite loop to check/wait till the application has launched.
@eel.expose
def start():

sly rover
mighty arch
#

take ur time im 100% sure that I will be stuck on this issue for a while

#

im still testing different behaviors and check/think of maybe some sort of cheapfix or work around

sly rover
#

I'm back that took a fair bit longer than anticipated

mighty arch
#

no worries

#

also feel free to ask questions about the code if needed

#

or ask me to test any suggestions you had in mind

sly rover
#

Unrelated side note just because I noticed it...

Instead of doing

import ctypes
user32 = ctypes.windll.user32

You know you can just do from ctypes.windll import user32?

mighty arch
#

true

#

ill do it

#

thanks for the note

#

uhh apparently thats not possible

#

odd

#

let me see

sly rover
#

Yeah that's weird... That should work

#

So uh, I think I understand your code now

#

At least what you're trying to do

mighty arch
#

cuz if the other app is fullscreen if you focus on my app the fullscreen app minimizes itself

sly rover
#

But I don't see any error that would be causing this behavior which is leading me to believe that this is some weird bug in windows or one of these modules.

The best I can suggest is to see if you could attempt to run this on a different version of windows and see if the same thing happens

mighty arch
#

sure ill try other apps

#

and its not a "bug"

#

the docs of the functionality of making an application window unfocusable just states the window becomes unfocusable

#

but does not mention the changed features/restrictions with it

#

so it doesnt explicitly state or mention that a mouse functions would work as always while keeping the application unfocused

#

it doesnt mention it should or shouldnt

#

same with the keyboard

mighty arch
#

the application becomes unfocusable

#

the application also allows me to press buttons etc and everything mouse related works fine

#

i cannot type in it

#

same problem as my own app

sly rover
mighty arch
#

im not sure if its a "bug"

#

or just a feature that is not supported to have x behavior based on the nature of the unfocus

#

i do find it odd that it supports mouse events

#

but no keyevents

#

and yeah i could create mousehooks and listeners etc etc

#

but i really do not want to go that route

#

tbh i might have an idea....

#

a simple one without hooks

#
  1. Create a javascript listener that listens to focused textfields.
  2. If text field is focused call the focused function in python else call the unfocused function.
  3. unfocused basically sets a global variable focused to false
  4. Focused checks the following:
  • if the current focused app window is the application that the overlay is made for
  • if the overlay app is not hidden (since you will be able to toggle it on and off)
#

this way i can create a listener through javascript that doesnt require me to use windows listeners

#

i will be able to detect when the text field and even which textfield is currently focused

#

and then retrieve keystrokes and send key pressed data to the client to show it

#

i will try this out and see if this works

#

if this works i might have solved this problem in a unique way since a lot of people online have stated this issue with 0 fix to it!!!! jgfdkjdsf

#

but due to the nature of how my app works i could possibly just fix it with a "cheap fix"

#

imma try it out rn

#

if it works i will keep this cheap fix for now untill i found a proper/cleaner way of doing it

mighty arch
#

@sly rover

#

i can detect now if i am focused or not on any textfield

#

and on top of this i can also retrieve which field is selected

#

now what i have to do is send all of my key events to to the textfields as long as the field is focused

#

so i found out a cheap fix

shrewd spear
#

It is necessary, because if you don't specify them, then python try to "guess" the parameter and it can cause multiple issue

mighty arch
#

i don't understand what you mean

#

like what is this usefull for and why is it recommended?

shrewd spear
mighty arch
#

i found this in the win32 api

#

where they are talking about an overlay ontop of any app regardless of their exclusive fullscreen rights

shrewd spear
ivory juncoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.