Qestion :
Est il possible d'intercepter raccourcis clavier de Windows, du type Ctrl+Alt+Suppr ou Alt+Tab?
L'aide de purebasic (PB) nous donne 2 solutions : avec ou sans DirectX (Sous WIndows évidemment).
Sans directX, on peut créer et donc intercepter les raccourcis avec :
AddKeyboardShortcut() (voir l'aide de PB)
Faisons un test.
Code : Tout sélectionner
Enumeration ;fenêtre
#fenetre_0
EndEnumeration
Enumeration ;gadget
#EditorGadget_0
EndEnumeration
If OpenWindow(#fenetre_0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(#EditorGadget_0, 8, 8, 306, 133)
For a = 0 To 5
AddGadgetItem(#EditorGadget_0, a, "Ligne "+Str(a))
Next
;AddKeyboardShortcut(#Fenetre, Raccourci, Event)
AddKeyboardShortcut(#fenetre_0, #PB_Shortcut_Control | #PB_Shortcut_F, 1) ; Crée un raccourci clavier CTRL+F sur la fenêtre #fenetre_0
AddKeyboardShortcut(#fenetre_0, #PB_Shortcut_Control | #PB_Shortcut_Alt|#PB_Shortcut_Delete, 2) ; Crée un raccourci clavier CTRL+ALT+SUPPR sur la fenêtre #fenetre_0
AddKeyboardShortcut(#fenetre_0, #PB_Shortcut_Control | #PB_Shortcut_Alt, 3) ; Crée un raccourci clavier CTRL+ALT sur la fenêtre #fenetre_0
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #EditorGadget_0 : Debug "EditorGadget_0 cliqué!"
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case 1 : Debug "CTRL+F"
Case 2 : Debug "CTRL+ALT+SUPPR" ;ne fonctionne pas, il faut passer par l'api
Case 3 : Debug "CTRL+ALT";;ne fonctionne pas, il faut passer par l'api
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
;RemoveKeyboardShortcut(#fenetre_0, #PB_Shortcut_All )
Aïe...
Les raccourcis simples comme CTRL+F fonctionnent mais pas les raccourcis "systèmes" comme CTRL+ALT+DEL
Essayons la version avec DirectX
Code : Tout sélectionner
Enumeration
#ARS : #info
EndEnumeration
If InitSprite() = 0
MessageRequester("Erreur", "Impossible d'ouvrir l'écran & l'environnement nécessaire aux sprites !", 0)
End
EndIf
InitKeyboard()
If OpenWindow(#ARS, 456, 257, 279, 200, "Test", #PB_Window_SystemMenu | #PB_Window_TitleBar )
TextGadget(#info, 10, 160, 260, 50, "Essaye le CTRL gauche +ALT droit +SUPPR", #PB_Text_Center)
OpenWindowedScreen(WindowID(#ARS), 0, 0, 160, 160, 0, 0, 0)
EndIf
Repeat
ExamineKeyboard()
If KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_RightAlt) And KeyboardPushed(#PB_Key_Delete)
MessageRequester("Bing :)","CTRL ALT SUP !")
End
EndIf
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Select EventWindow()
Case #ARS
CloseWindow(#ARS)
Break
EndSelect
EndSelect
ForEver
Aïe, ça ne marche pas non plus.
A priori, PB ne gère pas les raccourcis systèmes.
Il faut passer par l'api Windows et pour cela la version de démonstration de PB ne suffit pas, il faut acheter la licence.
Si c'est déjà le cas alors voyons ce que nous dit google.fr avec "purebasic Ctrl+Alt+"
Parmi les résultats se trouve en 1ère ligne :
http://www.purebasic.fr/english/viewtop ... f=5&t=5470
Le code date de 2003, mmmm...
Il faut l'adapter à PV4.xx
Code : Tout sélectionner
;
; by Danilo, Posted - 26 Sep 2002 : 03:14:34 (engl. forum)
;
; merendo,
;
; on winNT/2000/XP no program is allowed To
; block the CTRL+ALT+DEL, because this would
; be a security risc.
; Every program could block CTRL+ALT+DEL And
; do what it wants... And the user couldnt stop
; it anymore.
;
; If you use Windows98, you can do it by telling
; windows your app is a screensaver.
;
; This code disables ALT+TAB, ATL+ESC, And Windows-Keys...
; ...also on Win2000:
; The line " SystemParametersInfo_( #SPI_SCREENSAVERRUNNING, 1, @Dummy, 0) "
; is For Windows 9x Systems.
; It tells Windows that a screensaver is running, so it blocks
; all other input on Win9x.
SystemParametersInfo_( #SPI_SCREENSAVERRUNNING, 1, @Dummy, 0);
OpenLibrary(1,"kernel32.dll")
CallFunction(1,"RegisterServiceProcess", GetCurrentProcessId_(), 1 )
Structure KBDLLHOOKSTRUCT
vkCode.l
scanCode.l
flags.l
time.l
dwExtraInfo.l
EndStructure
Global hook
Procedure.l myKeyboardHook(nCode, wParam, *p.KBDLLHOOKSTRUCT)
If nCode = #HC_ACTION
If wParam = #WM_KEYDOWN Or wParam = #WM_SYSKEYDOWN Or wParam = #WM_KEYUP Or wParam = #WM_SYSKEYUP
#LLKHF_ALTDOWN = $20
If ((*p\vkCode = #VK_TAB) And (*p\flags & #LLKHF_ALTDOWN)) Or ((*p\vkCode = #VK_ESCAPE) And (*p\flags & #LLKHF_ALTDOWN)) Or ((*p\vkCode & #VK_ESCAPE) And (GetKeyState_(#VK_CONTROL) & $8000)) ;Or
ProcedureReturn 1
ElseIf (*p\vkCode = #VK_LWIN) Or (*p\vkCode = #VK_RWIN)
ProcedureReturn 1
EndIf
EndIf
EndIf
;beep_(800,1)
ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam)
EndProcedure
; Win NT
#WH_KEYBOARD_LL = 13
hook = SetWindowsHookEx_(#WH_KEYBOARD_LL,@myKeyboardHook(),GetModuleHandle_(0),0)
;If hook = 0: End: EndIf
hWnd = OpenWindow(1,10,10,100,100,"",#PB_Window_BorderLess)
ShowWindow_(hWnd, #SW_MAXIMIZE)
;SetWinBackgroundColor_(hWnd,0)
;CreateGadgetList(hWnd)
hString = StringGadget(1,GetSystemMetrics_(#SM_CXSCREEN)/2-100/2, GetSystemMetrics_(#SM_CYSCREEN)/2,100,20,"Enter Password",#PB_String_Numeric)
SetFocus_(hString)
GetWindowRect_(hString, winrect.RECT)
;SetThreadPriority_(GetCurrentThread_(),#REALTIME_PRIORITY_CLASS)
Repeat
Message = WindowEvent()
SetActiveWindow_(hWnd)
SetWindowPos_(hWnd,#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
ClipCursor_(winrect)
SendMessage_(hstring,#WM_KEYDOWN,#VK_END,0)
SetFocus_(hString)
If Message
Select Message
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If GetGadgetText(1) = "123"
UnhookWindowsHookEx_(hook)
ClipCursor_(0)
SystemParametersInfo_( #SPI_SCREENSAVERRUNNING, 0, @Dummy, 0);
End
EndIf
EndSelect
EndSelect
Else
Delay(1)
EndIf
ForEver
; But CTRL+ALT+DEL can on win2000 only be disabled
; by writing a lowlevel system device driver For the keyboard.
; Info about this should be in the MS DDK.
;
; Best way For you would be To install win98
; on your disco machine...
;
; cya,
; ...Danilo
;
; (registered PureBasic user)
;
Ça marche ! le ctrl alt suppr est désactivé en utilisant un hook clavier.
Pour sortir de la fenêtre, taper 123.
Marche avec ALT+TAB, ATL+ESC, et Windows-Keys sous Windows XP.
A verifier sous W7.
En cherchant mieux, il existe peut-être une meilleure solution.
Mesa.