当前位置: 首页 > news >正文

linux版本微信打开关闭快捷键

安装相关依赖:

sudo apt install libx11-dev libdbus-1-dev wmctrl

复制源代码,编译:

g++ -std=c++11 -o wechat wechat.cpp

deepin系统设置全局快捷键
image

源码:

//g++ -std=c++11 -o wechat wechat.cpp
//这是一个通过alt+w实现微信显示和隐藏的c++语言程序
//sudo apt install wmctrl
//libx11-dev libdbus-1-dev 
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>class WeChatController {
private:std::vector<std::string> executeCommand(const std::string& cmd) {std::vector<std::string> result;FILE* pipe = popen(cmd.c_str(), "r");if (!pipe) return result;char buffer[128];while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {std::string line(buffer);if (!line.empty() && line.back() == '\n') {line.pop_back();}result.push_back(line);}pclose(pipe);return result;}std::string getWeChatPid() {auto pids = executeCommand("pgrep -x wechat");return pids.empty() ? "" : pids[0];}std::vector<std::string> getWeChatWindows() {return executeCommand("xdotool search --class 'wechat'");}std::string getActiveWindow() {auto windows = executeCommand("xdotool getactivewindow");return windows.empty() ? "" : windows[0];}void activateWeChatTrayIcon() {std::string wechat_pid = getWeChatPid();if (wechat_pid.empty()) {exit(1);}auto items = executeCommand("qdbus org.kde.StatusNotifierWatcher /StatusNotifierWatcher org.kde.StatusNotifierWatcher.RegisteredStatusNotifierItems");for (const auto& item : items) {if (item.find(wechat_pid) != std::string::npos) {size_t pos = item.find('/');if (pos != std::string::npos) {std::string item_name = item.substr(0, pos);std::string cmd = "dbus-send --session --type=method_call --dest=\"" + item_name + "\" /StatusNotifierItem org.kde.StatusNotifierItem.Activate int32:0 int32:0";system(cmd.c_str());break;}}}}public:void run() {// 获取微信窗口和当前活动窗口auto wechat_windows = getWeChatWindows();std::string current_window = getActiveWindow();bool found = false;std::string active_wechat_window;// 检查当前活动窗口是否是微信窗口for (const auto& window_id : wechat_windows) {if (current_window == window_id) {found = true;active_wechat_window = window_id;break;}}// 切换逻辑if (found) {// 微信窗口是当前活动窗口,关闭它if (system("which wmctrl > /dev/null 2>&1") == 0) {std::string cmd = "wmctrl -ic " + active_wechat_window;system(cmd.c_str());} else {// 备用方案:发送 Alt+F4std::string cmd = "xdotool key --window " + active_wechat_window + " alt+F4";system(cmd.c_str());}} else {// 微信窗口不是当前活动窗口,激活第一个微信窗口if (!wechat_windows.empty()) {std::string cmd = "xdotool windowactivate " + wechat_windows[0];system(cmd.c_str());activateWeChatTrayIcon();} else {std::cout << "未找到微信窗口" << std::endl;}}}
};int main() {WeChatController controller;controller.run();return 0;
}
http://www.jsqmd.com/news/38712/

相关文章:

  • Linux shell映射表(变量的变量)
  • 详细介绍:显卡算力过高导致PyTorch不兼容的救赎指南
  • 2025/11/13
  • Linux《网络基础》 - 教程
  • 《程序员修炼之道》阅读笔记4
  • 记一次 .NET 某医联体管理系统 崩溃分析
  • 如何构建可信智能 Data Agent?推荐 Aloudata Agent 分析决策智能体
  • #题解#牛客:牛牛的构造#DP#构造#
  • Machine Learning - SVM Part 2: The Radial Kernel
  • 2025-11-12 ZYZ28-NOIP-aoao round 2 hetao1733837的record
  • 2025/11/12
  • redis stream介绍
  • Java 线性表、栈、队列和优先队列
  • 2025/11/11
  • 植物大战僵尸修改器下载教程:图文详解与实用技巧
  • 微服务——注册中心
  • 【深度学习计算机视觉】13:实战Kaggle比赛:图像分类 (CIFAR-10) - 指南
  • fabricjs 整合 vue3-sketch-ruler 实现标尺功能
  • 2025年真空耙式干燥机定做厂家权威推荐榜单:真空单锥螺带干燥机/沸腾床干燥机/闪蒸干燥机源头厂家精选
  • 基础查找算法(三)二分查找
  • 2025年软像套电缆订做厂家权威推荐榜单:补偿电缆/矿物质电缆/电力电缆源头厂家精选
  • 2025年济南统招专升本学校权威推荐榜单:专升本机构报名/全日制专升本/专升本考试培训学校精选
  • 一些水题
  • (3)Bug篇 - 详解
  • 西林瓶灌装轧盖机:黔东南折旧年限与成本解析
  • list对象 集合 和 String 互转
  • 碎碎念(二四)
  • 高精度除法模板(p1480)
  • 如何完成一个简单的rust WebAssembly调用
  • 焊接工业机器人节气装置