📄 hotkey_win.cpp
字号:
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* copyright : (C) 2003 by Zhang Yong *
* email : z-yong163@163.com *
***************************************************************************/
#include <windows.h>
#include "hotkeyitem.h"
#include "hotkeywidget.h"
#include "hotkey.h"
#include <qlistview.h>
#include <qradiobutton.h>
#include <qlineedit.h>
class HotkeyListItem : public QListViewItem {
public:
HotkeyListItem(QListView *parent, HotkeyItem *key);
bool isDefault();
bool isNone() {
return ((QString) shortcut).isEmpty();
}
void save() {
hotkey->keySeq = shortcut;
}
QKeySequence shortcut;
HotkeyItem *hotkey;
};
class WinHotkeyWidget : public HotkeyWidget {
public:
WinHotkeyWidget(QWidget *parent);
virtual void showEvent(QShowEvent *e);
virtual void slotSelChanged();
virtual void slotDefaultKey(bool sel);
virtual void slotNoneKey(bool sel);
void addHotkey(HotkeyItem *key);
void save();
private:
HotkeyListItem *lastHotkey;
};
class WinHotkey : public QWidget, public Hotkey {
public:
WinHotkey();
virtual QString getKey(const char *name);
virtual void setKey(const char *name, const QString &key);
virtual void insert(const char *name, const QString &desc,
const QKeySequence &def, const QObject *obj, const char *slot);
virtual QWidget *getConfigWidget(QWidget *parent);
virtual void saveConfig(QWidget *w) {
((WinHotkeyWidget *) w)->save();
}
virtual void update();
private:
bool winEvent(MSG *msg);
HotkeyItem *findKey(const char *name);
QPtrList<HotkeyItem> hotkeyList;
int idNext;
};
Hotkey *Hotkey::instance()
{
static WinHotkey *key;
if (!key)
key = new WinHotkey;
return key;
}
HotkeyListItem::HotkeyListItem(QListView *parent, HotkeyItem *key)
: QListViewItem(parent)
{
hotkey = key;
shortcut = key->keySeq;
setText(0, key->keyDesc);
setText(1, key->keySeq);
}
bool HotkeyListItem::isDefault()
{
int id1 = (shortcut & ~UNICODE_ACCEL);
int id2 = (hotkey->keyDef & ~UNICODE_ACCEL);
return (id1 == id2);
}
HotkeyItem::HotkeyItem(const char *name, const QString &desc, const QKeySequence &def, int id)
{
keyName = name;
keyDesc = desc;
keySeq = keyDef = def;
keyId = id;
}
UINT HotkeyItem::getModifier()
{
UINT mod = 0;
if (keySeq & CTRL)
mod |= MOD_CONTROL;
if (keySeq & SHIFT)
mod |= MOD_SHIFT;
if (keySeq & ALT)
mod |= MOD_ALT;
return mod;
}
UINT HotkeyItem::getVirtualKey()
{
int key = (keySeq & 0xffff);
if (key >= Key_0 && key <= Key_9)
return (0x30 + (key - Key_0));
if (key >= Key_A && key <= Key_Z)
return (0x41 + (key - Key_A));
if (key >= Key_F1 && key <= Key_F24)
return (0x70 + (key - Key_F1));
return 0;
}
WinHotkey::WinHotkey()
{
idNext = 0x1000;
hotkeyList.setAutoDelete(TRUE);
}
HotkeyItem *WinHotkey::findKey(const char *name)
{
for (HotkeyItem *key = hotkeyList.first(); key; key = hotkeyList.next()) {
if (key->keyName == name)
return key;
}
return NULL;
}
QString WinHotkey::getKey(const char *name)
{
HotkeyItem *key = findKey(name);
if (!key)
return QString::null;
return key->keySeq;
}
void WinHotkey::setKey(const char *name, const QString &key)
{
HotkeyItem *item = findKey(name);
if (!item)
return;
item->keySeq = QKeySequence(key);
}
void WinHotkey::insert(const char *name, const QString &desc,
const QKeySequence &def, const QObject *obj, const char *slot)
{
HotkeyItem *key = findKey(name);
ASSERT(key == NULL);
key = new HotkeyItem(name, desc, def, idNext++);
connect(key, SIGNAL(keyPressed()), obj, slot);
hotkeyList.append(key);
}
QWidget *WinHotkey::getConfigWidget(QWidget *parent)
{
WinHotkeyWidget *w = new WinHotkeyWidget(parent);
for (HotkeyItem *key = hotkeyList.first(); key; key = hotkeyList.next())
w->addHotkey(key);
return w;
}
void WinHotkey::update()
{
for (HotkeyItem *key = hotkeyList.first(); key; key = hotkeyList.next()) {
UnregisterHotKey(winId(), key->keyId);
RegisterHotKey(winId(), key->keyId, key->getModifier(), key->getVirtualKey());
}
}
bool WinHotkey::winEvent(MSG *msg)
{
if (msg->message != WM_HOTKEY)
return FALSE;
for (HotkeyItem *key = hotkeyList.first(); key; key = hotkeyList.next()) {
if (key->keyId == (int) msg->wParam)
break;
}
if (!key)
return FALSE;
key->emitEvent();
return TRUE;
}
WinHotkeyWidget::WinHotkeyWidget(QWidget *parent)
: HotkeyWidget(parent)
{
lastHotkey = 0;
}
void WinHotkeyWidget::addHotkey(HotkeyItem *key)
{
HotkeyListItem *i = new HotkeyListItem(listView, key);
}
void WinHotkeyWidget::save()
{
slotSelChanged();
for (QListViewItem *i = listView->firstChild(); i; i = i->nextSibling())
((HotkeyListItem *) i)->save();
}
void WinHotkeyWidget::showEvent(QShowEvent *e)
{
listView->setSelected(listView->firstChild(), TRUE);
}
void WinHotkeyWidget::slotSelChanged()
{
if (lastHotkey)
lastHotkey->shortcut = QKeySequence(hotkeyEdit->text());
HotkeyListItem *key = (HotkeyListItem *) listView->currentItem();
if (key->isDefault())
defaultRadio->setChecked(TRUE);
else if (key->isNone())
noneRadio->setChecked(TRUE);
else
customRadio->setChecked(TRUE);
hotkeyEdit->setText(key->shortcut);
hotkeyEdit->setEnabled(customRadio->isChecked());
lastHotkey = key;
}
void WinHotkeyWidget::slotDefaultKey(bool sel)
{
if (sel) {
HotkeyListItem *key = (HotkeyListItem *) listView->currentItem();
hotkeyEdit->setText(key->hotkey->keyDef);
}
}
void WinHotkeyWidget::slotNoneKey(bool sel)
{
if (sel)
hotkeyEdit->clear();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -