Skip to content
The Chat Archive
gpt-4o-mini-2024-07-18· OpenAI WildChat-4.8M (AI2)

in autohotkey how can i make mouse co-ord clicks scale between different resolutions

  1. User

    in autohotkey how can i make mouse co-ord clicks scale between different resolutions

  2. Assistant

    When working with AutoHotkey (AHK) to simulate mouse clicks across different screen resolutions, a good approach is to normalize the coordinates based on the current screen resolution. By calculating relative positions based on the width and height of the screen, you can ensure that your mouse clicks will work as intended regardless of the resolution.

    Here’s a basic example of how to achieve this:

    1. Determine the target coordinates: The coordinates where you want to click can be defined based on a percentage of the screen's width and height.
    2. Get the current screen resolution: Use A_ScreenWidth and A_ScreenHeight to find the current dimensions.
    3. Calculate the actual coordinates: Scale the target coordinates by the current resolution.

    Here is a sample AHK script that demonstrates this:

    ; Define the target coordinates (as a percentage of screen size)
    TargetXPercent := 0.5  ; 50% of width
    TargetYPercent := 0.5  ; 50% of height
    
    ; Get the current screen resolution
    ScreenWidth := A_ScreenWidth
    ScreenHeight := A_ScreenHeight
    
    ; Calculate the actual coordinates
    TargetX := ScreenWidth * TargetXPercent
    TargetY := ScreenHeight * TargetYPercent
    
    ; Move the mouse to the calculated coordinates
    MouseMove, TargetX, TargetY
    
    ; Click the left mouse button
    Click
    

    Explanation:

    1. Target Coordinates: The values TargetXPercent and TargetYPercent represent where you want to click on the screen, as a fraction of the screen dimensions (0.0 to 1.0).
    2. Screen Resolution: A_ScreenWidth and A_ScreenHeight retrieve the dimensions of the current screen.
    3. Target Calculation: The actual target position is computed by multiplying the screen dimensions by the percentage coordinates.
    4. MouseMove and Click: The script moves the mouse to the calculated coordinates and performs a click.

    Example Use Case:

    If you wanted to click at the center of the screen, you would set both TargetXPercent and TargetYPercent to 0.5. For clicking at the top-left corner, you would set both values to 0.

    Adapting for Different Resolutions:

    To adapt your script for various resolutions, simply change the TargetXPercent and TargetYPercent values. The script will calculate the appropriate coordinates dynamically based on the current screen resolution. This method ensures that the positions scale correctly when executed on screens with different resolutions.