Active the shortcut for windows

This commit is contained in:
Ximi1970
2023-12-06 21:47:52 +01:00
parent a474b38aff
commit 8334c67ece
3 changed files with 138 additions and 11 deletions

View File

@@ -10,6 +10,81 @@
* Qt includes
*/
#include <QKeySequence>
#include <QMap>
/*
* Define the statics
*/
const QMap< Qt::Key, int > NativeEventFilterWin::m_virtual_key_map {
{ Qt::Key_Escape, VK_ESCAPE },
{ Qt::Key_Tab, VK_TAB },
{ Qt::Key_Backtab, VK_TAB },
{ Qt::Key_Backspace, VK_BACK },
{ Qt::Key_Return, VK_RETURN },
{ Qt::Key_Enter, VK_RETURN },
{ Qt::Key_Insert, VK_INSERT },
{ Qt::Key_Delete, VK_DELETE },
{ Qt::Key_Pause, VK_PAUSE },
{ Qt::Key_Print, VK_PRINT },
//{ Qt::Key_SysReq, ... },
{ Qt::Key_Clear, VK_CLEAR },
{ Qt::Key_Home, VK_HOME },
{ Qt::Key_End, VK_END },
{ Qt::Key_Left, VK_LEFT },
{ Qt::Key_Up, VK_UP },
{ Qt::Key_Right, VK_RIGHT },
{ Qt::Key_Down, VK_DOWN },
{ Qt::Key_PageUp, VK_PRIOR },
{ Qt::Key_PageDown, VK_NEXT },
//{Qt::Shift, ...},
//{Qt::Control, ...},
//{Qt::Meta, ...},
//{Qt::Alt, ...},
//{Qt::AltGr, ...},
{ Qt::Key_CapsLock, VK_CAPITAL },
{ Qt::Key_NumLock, VK_NUMLOCK },
{ Qt::Key_ScrollLock, VK_SCROLL },
{ Qt::Key_F1, VK_F1 },
{ Qt::Key_F2, VK_F2 },
{ Qt::Key_F3, VK_F3 },
{ Qt::Key_F4, VK_F4 },
{ Qt::Key_F5, VK_F5 },
{ Qt::Key_F6, VK_F6 },
{ Qt::Key_F7, VK_F7 },
{ Qt::Key_F8, VK_F8 },
{ Qt::Key_F9, VK_F9 },
{ Qt::Key_F10, VK_F10 },
{ Qt::Key_F11, VK_F11 },
{ Qt::Key_F12, VK_F12 },
{ Qt::Key_F13, VK_F13 },
{ Qt::Key_F14, VK_F14 },
{ Qt::Key_F15, VK_F15 },
{ Qt::Key_F16, VK_F16 },
{ Qt::Key_F17, VK_F17 },
{ Qt::Key_F18, VK_F18 },
{ Qt::Key_F19, VK_F19 },
{ Qt::Key_F20, VK_F20 },
{ Qt::Key_F21, VK_F21 },
{ Qt::Key_F22, VK_F22 },
{ Qt::Key_F23, VK_F23 },
{ Qt::Key_F24, VK_F24 },
//{ Qt::Key_F25, ... },
//{ Qt::Key_F26, ... },
//{ Qt::Key_F27, ... },
//{ Qt::Key_F28, ... },
//{ Qt::Key_F29, ... },
//{ Qt::Key_F30, ... },
//{ Qt::Key_F31, ... },
//{ Qt::Key_F32, ... },
//{ Qt::Key_F33, ... },
//{ Qt::Key_F34, ... },
//{ Qt::Key_F35, ... },
{ Qt::Key_MediaNext, VK_MEDIA_NEXT_TRACK },
{ Qt::Key_MediaPrevious, VK_MEDIA_PREV_TRACK },
{ Qt::Key_MediaPlay, VK_MEDIA_PLAY_PAUSE },
{ Qt::Key_MediaStop, VK_MEDIA_STOP },
};
/*
@@ -20,11 +95,10 @@ bool NativeEventFilterWin::nativeEventFilter( const QByteArray& eventType, void*
Q_UNUSED( eventType )
Q_UNUSED( result )
MSG* msg = reinterpret_cast< MSG* >( message );
MSG* msg = static_cast< MSG* >( message );
if( msg->message == WM_HOTKEY )
{
if( msg->wParam == 100 )
if( msg->wParam == SHORTCUT_ID )
{
activated();
return true;
@@ -52,14 +126,53 @@ bool NativeEventFilterWin::connectShortcut( QKeySequence key_seq )
*/
bool NativeEventFilterWin::connectShortcut( Qt::Key key_code, Qt::KeyboardModifiers key_modifiers )
{
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
m_key_code_win = 0;
if( key_code <= 0xffff )
{
SHORT virtual_key = VkKeyScanW( key_code );
if( virtual_key > -1 )
{
m_key_code_win = virtual_key & 0xff;
}
}
if( m_key_code_win == 0 && m_virtual_key_map.contains( key_code ) )
{
m_key_code_win = m_virtual_key_map[ key_code ];
}
else
{
if( key_code <= 0xffff )
{
m_key_code_win = key_code & 0xff;
}
}
// Set windows callback
RegisterHotKey( (HWND)MainWindow::winId(), // Set the system identifier of the widget window that will handle the HotKey
100, // Set identifier HotKey
MOD_ALT | MOD_SHIFT, // Set modifiers
'D' ); // We define hotkeys for HotKey
/*
* Convert the Qt key modifiers to Windows key modifiers
*/
m_key_modifiers_win = 0;
if( key_modifiers & Qt::ShiftModifier )
{
m_key_modifiers_win |= MOD_SHIFT;
}
if( key_modifiers & Qt::ControlModifier )
{
m_key_modifiers_win |= MOD_CONTROL;
}
if( key_modifiers & Qt::AltModifier )
{
m_key_modifiers_win |= MOD_ALT;
}
if( key_modifiers & Qt::MetaModifier )
{
m_key_modifiers_win |= MOD_WIN;
}
/*
* Register the shortcut
*/
RegisterHotKey( (HWND)NULL, SHORTCUT_ID, m_key_modifiers_win + MOD_NOREPEAT, m_key_code_win );
return true;
}
@@ -70,8 +183,10 @@ bool NativeEventFilterWin::connectShortcut( Qt::Key key_code, Qt::KeyboardModifi
*/
bool NativeEventFilterWin::disconnectShortcut()
{
// Remove windows callback
UnregisterHotKey( (HWND)MainWindow::winId(), 100 );
/*
* Unregister the shortcut
*/
UnregisterHotKey( (HWND)NULL, SHORTCUT_ID );
return true;
}

View File

@@ -15,6 +15,13 @@
*/
/*
* Defines
*/
#define SHORTCUT_ID 1024
/**
* @brief The NativeEventFilterWin class
*/
@@ -73,6 +80,10 @@ class NativeEventFilterWin : public NativeEventFilterBase
*/
int m_key_code_win;
/**
* @brief m_virtual_key_map. Holds the Qt to Windows virtual key translation map.
*/
static const QMap< Qt::Key, int > m_virtual_key_map;
};
SINGLETON( NativeEventFilterWin )

View File

@@ -187,6 +187,7 @@ SysTrayX::SysTrayX( QObject *parent ) : QObject( parent )
/*
m_preferences->setBrowserVersion( "115.1.0" );
// m_preferences->setBrowserVersion( "102.2.3" );
m_preferences->setShowHideShortcut( QKeySequence( Qt::CTRL | Qt::Key_P ) );
slotLoadLanguage( "en-US" );
//slotLoadLanguage( "it" );