DtkDevice
DTK library for device.
载入中...
搜索中...
未找到
inputdevices

dtkinputdevices module

dtkinputdevices模块

Introduction

dtkinputdevices provides access to input devices and global input settings on deepin and uos.

Structure

There are 9 classes in this module:

  • DInputDeviceManager InputDevices manager, can be directly constructed
  • DInputDeviceSetting global settings, can be acquired from manager
  • DInputDevice abstract device base class (also generic device class), can be instantiated by manager
  • DInputDevicePointer abstract pointer device, as a abstract layer, it cannot be instantiated
  • DInputDeviceMouse mouse device, derived from DInputDevicePointer, can be instantiated by manager
  • DInputDeviceTouchPad touch pad device, derived from DInputDevicePointer, can be instantiated by manager
  • DInputDeviceTrackPoint track point device, derived from DInputDevicePointer, can be instantiated by manager
  • DInputDeviceTablet tablet pad and tablet tools device, derived from DInputDevicePointer, can be instantiated by manager
  • DInputDeviceKeyboard keyboard device, derived from DInputDevice, can be instantiated by manager

Example

Here is a simple example:

#include "dinputdevice.h"
#include <QCoreApplication>
#include <QObject>
DDEVICE_USE_NAMESPACE // use namespace
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
DInputDeviceManager manager; // create a manager
qDebug() << manager.deviceInfos();
QObject::connect(&manager, &DInputDeviceManager::deviceAdded, &app, [&](const DeviceInfo &info) {
qDebug() << "Device" << info << "removed.";
qInfo() << "Devices:" << manager.deviceInfos();
});
QObject::connect(&manager, &DInputDeviceManager::deviceRemoved, &app, [&](const DeviceInfo &info) {
qDebug() << "Device" << info << "removed.";
qInfo() << "Devices:" << manager.deviceInfos();
}); // monitor plug and unplug signals
auto infos = manager.deviceInfos(); // get all device infos
foreach (const auto &info, infos) {
auto expected = manager.createDevice(info); // create device by info
if (!expected) {
qWarning() << "Can't create device using info" << info << ". Error code:" << expected.error().getErrorCode() << ","
<< expected.error().getErrorMessage();
} else {
auto device = expected.value();
qInfo() << "Device id:" << device->id() << ", name:" << device->name() << ", type:" << device->type()
<< ", enabled:" << device->enabled();
switch (info.type) {
// cast to specific device by type
case DeviceType::Mouse: {
auto mouse = device.dynamicCast<DInputDeviceMouse>();
qDebug() << "Acceleration profile:" << mouse->accelerationProfile();
break;
}
case DeviceType::TouchPad: {
auto touchPad = device.dynamicCast<DInputDeviceTouchPad>();
qDebug() << "Disable while typing:" << touchPad->disableWhileTyping();
qDebug() << "Acceleration speed:" << touchPad->accelerationSpeed();
break;
}
case DeviceType::TrackPoint: {
auto trackPoint = device.dynamicCast<DInputDeviceTrackPoint>();
qDebug() << "Middle button timeout:" << trackPoint->middleButtonTimeout();
break;
}
case DeviceType::Tablet: {
auto tablet = device.dynamicCast<DInputDeviceTablet>();
qDebug() << "Cursor mode:" << tablet->cursorMode();
break;
}
case DeviceType::Keyboard:
case DeviceType::Generic:
default:
qWarning() << "These devices is not implemented or recognized.";
break;
}
}
}
auto eSetting = manager.setting();
if (!eSetting) {
qWarning() << "Can't get default setting. Error code:" << eSetting.error().getErrorCode() << ","
<< eSetting.error().getErrorMessage();
} else {
auto setting = eSetting.value();
qDebug() << "Cursor blink interval:" << setting->cursorBlinkInterval();
qDebug() << "Wheel speed:" << setting->wheelSpeed();
}
return app.exec();
}

项目介绍

dtkinputdevices是对deepin和uos上输入设备的封装,输入设备包括键盘、鼠标、触摸板、数位板、指针杆等。除此之外,dtkinputdevices还提供全局的输入相关设置的访问与修改。

项目结构

该模块一共提供九个类:

  • DInputDeviceManager InputDevices管理类,可直接构造
  • DInputDeviceSetting 全局设置,可通过管理类获取
  • DInputDevice 设备抽象基类(通用设备类),可实例化,但是只能通过管理类实例化
  • DInputDevicePointer 抽象指针设备类,继承自DInputDevice,DInputDevicePointer为中间抽象层,不可实例化
  • DInputDeviceMouse 鼠标设备类,继承自DInputDevicePointer,可通过管理类实例化
  • DInputDeviceTouchPad 触摸板设备类,继承自DInputDevicePointer,可通过管理类实例化
  • DInputDeviceTrackPoint 指针杆设备类,继承自DInputDevicePointer,可通过管理类实例化
  • DInputDeviceTablet 数位板设备类,继承自DInputDevicePointer,可通过管理类实例化
  • DInputDeviceKeyboard 键盘设备类,继承自DInputDevice,可通过管理类实例化

使用实例

以下是一个简单的例子:

#include "dinputdevice.h"
#include <QCoreApplication>
#include <QObject>
DDEVICE_USE_NAMESPACE // 该宏提供简单的使用命名空间的方式
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
DInputDeviceManager manager; // 创建manager
qDebug() << manager.deviceInfos();
QObject::connect(&manager, &DInputDeviceManager::deviceAdded, &app, [&](const DeviceInfo &info) {
qDebug() << "Device" << info << "removed.";
qInfo() << "Devices:" << manager.deviceInfos();
});
QObject::connect(&manager, &DInputDeviceManager::deviceRemoved, &app, [&](const DeviceInfo &info) {
qDebug() << "Device" << info << "removed.";
qInfo() << "Devices:" << manager.deviceInfos();
}); // 监听设备插拔信号
auto infos = manager.deviceInfos(); // 获取所有设备信息
foreach (const auto &info, infos) {
auto expected = manager.createDevice(info); // 使用设备信息创建设备
if (!expected) {
qWarning() << "Can't create device using info" << info << ". Error code:" << expected.error().getErrorCode() << ","
<< expected.error().getErrorMessage();
} else {
auto device = expected.value();
qInfo() << "Device id:" << device->id() << ", name:" << device->name() << ", type:" << device->type()
<< ", enabled:" << device->enabled();
switch (info.type) {
// 根据不同的设备类型进行相应的转换
case DeviceType::Mouse: {
auto mouse = device.dynamicCast<DInputDeviceMouse>();
qDebug() << "Acceleration profile:" << mouse->accelerationProfile();
break;
}
case DeviceType::TouchPad: {
auto touchPad = device.dynamicCast<DInputDeviceTouchPad>();
qDebug() << "Disable while typing:" << touchPad->disableWhileTyping();
qDebug() << "Acceleration speed:" << touchPad->accelerationSpeed();
break;
}
case DeviceType::TrackPoint: {
auto trackPoint = device.dynamicCast<DInputDeviceTrackPoint>();
qDebug() << "Middle button timeout:" << trackPoint->middleButtonTimeout();
break;
}
case DeviceType::Tablet: {
auto tablet = device.dynamicCast<DInputDeviceTablet>();
qDebug() << "Cursor mode:" << tablet->cursorMode();
break;
}
case DeviceType::Keyboard:
case DeviceType::Generic:
default:
qWarning() << "These devices is not implemented or recognized.";
break;
}
}
}
auto eSetting = manager.setting();
if (!eSetting) {
qWarning() << "Can't get default setting. Error code:" << eSetting.error().getErrorCode() << ","
<< eSetting.error().getErrorMessage();
} else {
auto setting = eSetting.value();
qDebug() << "Cursor blink interval:" << setting->cursorBlinkInterval();
qDebug() << "Wheel speed:" << setting->wheelSpeed();
}
return app.exec();
}