lua脚本控制鼠标速度

一:LUA脚本:就是用xinput来完成对鼠标速度的控制


function luaMouseSpeed(s)
        local cmdStr
        local pStr
        local pFile
        local message = ""
        
        cmdStr = "xinput --list |grep mouse"
        print("mouse speed cmd: ", cmdStr)
        
        pFile = io.popen(cmdStr)
        pStr = pFile:read("*all")
        pFile:close()

        local a = string.find(pStr, 'id')
        local id = (string.sub(pStr,a+3,56))
        message = "xinput --set-prop" .. " " ..id  .."Device Accel Constant Deceleration" .. " " .. s
        print("message:", message)
        os.execute(message)
end

C调用:

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main()
{
    char luaPath[128] = {0};
    int ret = 0;

    lua_State *luaHandle = luaL_newstate();
    luaL_openlibs(luaHandle);

    snprintf(luaPath, sizeof(luaPath),"/home/wdj/mouse.lua");
    ret = luaL_dofile(luaHandle, luaPath);
    if(ret != LUA_OK)
    {
        printf("\nerror luaL_dofile!\n");
    }

    int speed = 1000;
    lua_getglobal(luaHandle, "luaMouseSpeed");
    if(lua_isfunction(luaHandle, -1))
    {
        lua_pushnumber(luaHandle, speed);
    }
    else
    printf("\nerror lua_isfunction\n");

    lua_pcall(luaHandle, 1, 0, 0);
    lua_close(luaHandle);
    return 0;
}


编译:gcc main.c -I …/include -L …/lib/ -llua -lm -ldl
补充:
二:系统命令
有些linux版本的xinput表现不一致,很麻烦。

/* xset m speed */
int setMouseSpeed(int speed)
{
    char cmdStr[128] = {0};
    
    snprintf(cmdStr, sizeof(cmdStr), "xset m %d", speed);
    system(cmdStr);
    
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值