简体   繁体   English

如何在批处理脚本中更改鼠标设置?

[英]How do I change mouse settings in my batch script?

I'm trying to make a script that changes my mouse settings in the registry. 我试图制作一个脚本来更改注册表中的鼠标设置。 When I run the script I see that the changes are made, but they won't be taken into effect unless I log out and log back in, which is not feasible. 当我运行脚本时,我看到所做的更改,但是除非我注销并重新登录,否则更改将不会生效,这是不可行的。

:: MouseSensitivity                     10
:: MouseSpeed (Set Pointer Precision)   0
:: MouseThreshold1                      0
:: MouseThreshold2                      0

@ECHO OFF

REG ADD "HKEY_CURRENT_USER\Control Panel\Mouse" /v MouseSensitivity /t REG_SZ /d 10 /f
REG ADD "HKEY_CURRENT_USER\Control Panel\Mouse" /v MouseSpeed /t REG_SZ /d 0 /f
REG ADD "HKEY_CURRENT_USER\Control Panel\Mouse" /v MouseThreshold1 /t REG_SZ /d 0 /f
REG ADD "HKEY_CURRENT_USER\Control Panel\Mouse" /v MouseThreshold2 /t REG_SZ /d 0 /f

ECHO Execution logged on: %DATE% at %TIME% >> test.txt

:: /* Tried this, doesn't work.
:: RUNDLL32.EXE USER32.DLL, UpdatePerUserSystemParameters
:: */

@EXIT /B 0

Raymond Chen, you are right. Raymond Chen,你是对的。 Here's the solution I came up with using C++. 这是我使用C ++提出的解决方案。

#include <windows.h>

int main()
{
    int mouse_info[3];

    mouse_info[0] = 0;  // MouseThreshold1
    mouse_info[1] = 0;  // MouseThreshold2
    mouse_info[2] = 0;  // Set Pointer Precision

    // Set and update user settings. In effect immediately.
    SystemParametersInfo(SPI_SETMOUSE,
                         0,
                         mouse_info,
                         SPIF_UPDATEINIFILE);

    return 0;
}

Changing registry values won't apply the changes. 更改注册表值将不会应用更改。

The Windows system reads and loads them on startup, which doesn't happen on changing the values. Windows系统会在启动时读取并加载它们,而在更改值时不会发生这种情况。 You need to call system APIs to do that. 您需要调用系统API来执行此操作。

I've used a PowerShell script for same. 我已经使用了PowerShell脚本。 You can find the code at 您可以在以下位置找到代码

https://github.com/raevilman/windows-scripts/tree/master/mouse/speed https://github.com/raevilman/windows-scripts/tree/master/mouse/speed

There I have placed two batch files. 我在那里放置了两个批处理文件。 One for the touchpad and one for the USB mouse, because they both operate on different speeds. 一个用于触摸板,另一个用于USB鼠标,因为它们都以不同的速度运行。 You will definitely have your version as per needs. 您肯定会根据需要拥有自己的版本。

PS: Don't ask about PowerShell execution policy, etc. If you face them, google it. PS:不要问有关PowerShell执行策略的问题。如果遇到这些问题,请用Google搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM