Globaler Keyboard Hook (Win32)

Windowsspezifisches Forum , API ,..
Beiträge, die plattformübergreifend sind, gehören ins 'Allgemein'-Forum.
real
Beiträge: 468
Registriert: 05.10.2004 14:43

Beitrag von real »

Bei Windows-Programmen funktioniert das keybd_event natürlich. Aber wenn ich innerhalb eines Spiels, das über DirectInput seine Tasteninformationen bezieht eine Taste simuliere wird diese nicht erkannt. Also dachte ich daran, die LowLevelKeyboardProc zu manipulieren.
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8809
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 64 GB DDR4-3200
Ubuntu 24.04.2 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken

Beitrag von NicTheQuick »

Achso, das sollte man natürlich wissen. Aber da habe ich momentan keine Ahnung, wie man das machen könnte.
Tut mir Leid...
real
Beiträge: 468
Registriert: 05.10.2004 14:43

Beitrag von real »

Sorry, Falschaussage: Auch unter DirectInput kann man einen Tastendruck simulieren...

Code: Alles auswählen

If *p\vkCode = #VK_LWIN
  keybd_event_($4D, MapVirtualKey_($4D,0), 0, 0)
  keybd_event_($4D, MapVirtualKey_($4D,0), #KEYEVENTF_KEYUP, 0)
  ProcedureReturn 1
EndIf
:D
Agent
Beiträge: 296
Registriert: 13.09.2004 11:28
Kontaktdaten:

Beitrag von Agent »

Dazu eine Frage:

Wie kann ich denn in der oberen mykeyboardhook-procedure abfangen, wenn jemand z.b. STRG-V drückt?

Mit

If *p\vkCode = #VK_LCONTROL And *p\vkCode = #VK_V...

geht es jedenfalls nicht.

Kurz und gut: Wie soll das mit Tasten-Kombinationen funktionieren?
Agent_Sasori
It's not a bug - it's a feature!
http://www.StephenKalisch.de | http://www.ria-tec.com | http://www.dirsync.de
real
Beiträge: 468
Registriert: 05.10.2004 14:43

Beitrag von real »

Versuch mal:

Code: Alles auswählen

If ((*p\vkCode = #VK_V) And (GetKeyState_(#VK_CONTROL) & $8000))
...
Gruß
René
Agent
Beiträge: 296
Registriert: 13.09.2004 11:28
Kontaktdaten:

Beitrag von Agent »

Hm...

für was ist denn die $8000???? (hex ist klar, aber 8000)
Agent_Sasori
It's not a bug - it's a feature!
http://www.StephenKalisch.de | http://www.ria-tec.com | http://www.dirsync.de
Agent
Beiträge: 296
Registriert: 13.09.2004 11:28
Kontaktdaten:

Beitrag von Agent »

ah....


noch gedrückt.... kann das sein?
Agent_Sasori
It's not a bug - it's a feature!
http://www.StephenKalisch.de | http://www.ria-tec.com | http://www.dirsync.de
real
Beiträge: 468
Registriert: 05.10.2004 14:43

Beitrag von real »

"The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off - alternating each time the key is pressed)."

schau dir mal die infos im netz zum getkeystate an...
Benutzeravatar
R4z0r1989
Beiträge: 521
Registriert: 12.06.2005 18:06
Kontaktdaten:

Beitrag von R4z0r1989 »

*buddel* *ausgrab*

warum spert mein Code nicht F1 bis F12???

Code: Alles auswählen

; by Danilo, 07.04.2003 - german forum 
; 
Global hWindow,msg,hook

Structure KBDLLHOOKSTRUCT 
  vkCode.l 
  scanCode.l 
  flags.l 
  time.l 
  dwExtraInfo.l 
EndStructure 

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_F1 And *p\vkCode <= #VK_F12 ; F1 - F12 sperren!! 
            If *p\vkCode = #VK_F12 
              msg = 1
            EndIf
            ProcedureReturn 1 
         EndIf 
       EndIf 
    EndIf 
ProcedureReturn CallNextHookEx_(0, nCode, wParam, *p) 
EndProcedure 

Procedure AskForExit() 
  If MessageRequester("EXIT", "End the KeyboardHook ??",#MB_YESNO) = #IDYES 
     UnhookWindowsHookEx_(hook) : End 
  EndIf 
EndProcedure 

; Win NT 
#WH_KEYBOARD_LL = 13 
hook = SetWindowsHookEx_(#WH_KEYBOARD_LL,@myKeyboardHook(),GetModuleHandle_(0),0) 
If hook = 0: End: EndIf


Repeat 
  
  If msg = 1
    MessageRequester("lol","geht ned")
    AskForExit()
    msg = 0
  EndIf
  
ForEver
Edit: Warum verlangsamt mein Code auch noch die Tasta?
gibt es überhaupt eine möglichkeit es elegant ohne fenster zu lösen?
Benutzeravatar
Fluid Byte
Beiträge: 3110
Registriert: 27.09.2006 22:06
Wohnort: Berlin, Mitte

Beitrag von Fluid Byte »

Code: Alles auswählen

Structure KBDLLHOOKSTRUCT
	vkCode.l
	scanCode.l
	flags.l
	time.l
	dwExtraInfo.l
EndStructure 

Procedure KeyboardLLProc(nCode,wParam,*kbdhs.KBDLLHOOKSTRUCT)
	If nCode = #HC_ACTION And *kbdhs\vkCode >= #VK_F1 And *kbdhs\vkCode <= #VK_F12
		ProcedureReturn 1
	EndIf
	
	ProcedureReturn CallNextHookEx_(0,nCode,wParam,*kbdhs)
EndProcedure

hhkKeyboardLL = SetWindowsHookEx_(#WH_KEYBOARD_LL,@KeyboardLLProc(),GetModuleHandle_(0),0)

OpenWindow(0,0,0,320,240,"void",#WS_VISIBLE)

AddSysTrayIcon(0,WindowID(0),LoadImage(0,#PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico"))

Repeat
	EventID = WaitWindowEvent()
	
	If EventID = #PB_Event_SysTray And  EventType() = #PB_EventType_RightClick
		CreatePopupMenu(0)
		MenuItem(0,"Exit Keyboard Hook")
		DisplayPopupMenu(0,WindowID(0))
	EndIf
	
	If EventID = #PB_Event_Menu
		UnhookWindowsHookEx_(hhkKeyboardLL) : End
	EndIf
ForEver
Windows 10 Pro, 64-Bit / Outtakes | Derek
Antworten