- Offizieller Beitrag
Hi,
hier mal ein Bsp. wie man mittels Hook Umlaute und ß abfängt und durch ae... bzw ss ersetzt.Der Hook ist systemweit, also nicht auf ein Anwendungsfenster gerichtet.
Edit:
Hab das Bsp. nochmal erweitert.
- Der Hook gilt jetzt nur für ein Ctrl. auf der GUI.
- Mit LShift wird ersetzt zu Ae, Oe, Ue und mit RShift/CapsLock zu AE, OE, UE
Edit:
Ein weiteres Bsp.: Input Check
- In das Input können nur Ziffern und Punkt geschrieben werden (für Floatwerte)
- Komma wird zu Punkt gewandelt
Auto Ersetzen
#cs
Bsp. Auto-Ersetzen mit Hook
Umlaute und ß
linke Shift ergibt z.B. Ä-Ae, rechte Shift und CapsLock ergibt Ä-AE
Ä - Ae (AE) ä - ae
Ö - Oe (OE) ö - oe
Ü - Ue (UE) ü - ue
ß - ss
Hook ist nur wirksam für die GUI (Statusabfrage in der Schleife) und das benannte Ctrl.
#ce
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
Opt('MustDeclareVars', 1)
Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
Global $active = False, $isShift = False, $ctrl2Effect = 'Edit2' ; <== erfordert Classname !!
_Main()
[/autoit] [autoit][/autoit] [autoit]Func _Main()
Local $gui = GUICreate('Auto Ersetzen: Umlaute und "ß"', 400, 300)
GUICtrlCreateLabel('Im Input keine Ersetzung', 10, 10, 380)
GUICtrlCreateInput('', 10, 30, 380) ; Classname: Edit1 (Class: Edit, Ctrl: 1)
GUICtrlCreateLabel('Im Edit werden ersetzt: ä ==> ae ö ==> oe ü ==> ue ß ==> ss', 10, 60, 380)
Local $edit = GUICtrlCreateEdit('', 10, 80, 380, 210) ; Classname: Edit2 (Class: Edit, Ctrl: 2)
GUISetState()
Do
If BitAND(WinGetState($gui), And ControlGetFocus($gui) = $ctrl2Effect Then
$active = True
Else
$active = False
EndIf
Until GUIGetMsg() = -3
EndFunc
Func _Exit()
Exit
EndFunc
;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS, $vkCode, $ret, $isCapsLock = 0
$ret = DllCall("user32.dll","long","GetKeyState","long", 0x14)
If $ret[0] Then $isCapsLock = 1
$tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Or Not $active Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
$vkCode = DllStructGetData($tKEYHOOKS, "vkCode")
If $wParam = $WM_KEYDOWN Then
If $vkCode = 0xA0 Or $vkCode = 0xA1 Then
$isShift = True
EndIf
Switch $vkCode
Case 0xBA ; ü ==> ue
If $isShift Then
_keybd_event(0x55)
_keybd_event(0xA0, 0x2)
Else
_keybd_event(0x55)
EndIf
_keybd_event(0x45)
Return -1
Case 0xC0 ; ö ==>oe
If $isShift Then
_keybd_event(0x4F)
_keybd_event(0xA0, 0x2)
Else
_keybd_event(0x4F)
EndIf
_keybd_event(0x45)
Return -1
Case 0xDB ; ß ==> ss
If $isShift Or $isCapsLock Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
_keybd_event(0x53)
_keybd_event(0x53)
Return -1
Case 0xDE ; ä ==> ae
If $isShift Then
_keybd_event(0x41)
_keybd_event(0xA0, 0x2)
Else
_keybd_event(0x41)
EndIf
_keybd_event(0x45)
Return -1
EndSwitch
Else
Switch DllStructGetData($tKEYHOOKS, "flags")
Case 0x80, 0x81 ; 0x80= UP für 'normale' Tasten u. li. paarige, 0x81= UP für re. paarige
If $vkCode = 0xA0 Or $vkCode = 0xA1 Then $isShift = False ; LShift, RShift
EndSwitch
EndIf
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc
Func _keybd_event($vkCode, $Flag=0)
DllCall('user32.dll', 'int', 'keybd_event', 'int', $vkCode, 'int', 0, 'int', $Flag, 'ptr', 0)
EndFunc
Func OnAutoItExit()
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)
EndFunc
Input Check
#cs
Bsp. Input-Check mit Hook
nur Ziffern und Punkt erlaubt, Komma wird zu Punkt
Hook ist nur wirksam für die GUI (Statusabfrage in der Schleife) und das benannte Ctrl.
#ce
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
Opt('MustDeclareVars', 1)
Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
Global $active = False, $ctrl2Effect = 'Edit1' ; <== erfordert Classname !!
_Main()
[/autoit] [autoit][/autoit] [autoit]Func _Main()
Local $gui = GUICreate('Auto Ersetzen: Umlaute und "ß"', 400, 300)
GUICtrlCreateLabel('Im Input sind nur Ziffern und Punkt möglich, Komma wird zu Punkt', 10, 10, 380)
GUICtrlCreateInput('', 10, 30, 380) ; Classname: Edit1 (Class: Edit, Ctrl: 1)
GUICtrlCreateLabel('Im Edit kann frei geschrieben werden', 10, 60, 380)
Local $edit = GUICtrlCreateEdit('', 10, 80, 380, 210) ; Classname: Edit2 (Class: Edit, Ctrl: 2)
GUISetState()
Do
If BitAND(WinGetState($gui), And ControlGetFocus($gui) = $ctrl2Effect Then
$active = True
Else
$active = False
EndIf
Until GUIGetMsg() = -3
EndFunc
Func _Exit()
Exit
EndFunc
;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS, $vkCode, $aSafedKey[250]
For $i = 48 To 57 ; 0-9
$aSafedKey[$i] = 1
Next
$aSafedKey[188] = 1 ; ','
$aSafedKey[190] = 1 ; '.'
$tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Or Not $active Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
$vkCode = DllStructGetData($tKEYHOOKS, "vkCode")
If $wParam = $WM_KEYDOWN Then
If Not $aSafedKey[$vkCode] Then
_keybd_event(0x0)
Return -1
ElseIf $vkCode = 0xBC Then
_keybd_event(0xBE)
Return -1
EndIf
EndIf
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc
Func _keybd_event($vkCode, $Flag=0)
DllCall('user32.dll', 'int', 'keybd_event', 'int', $vkCode, 'int', 0, 'int', $Flag, 'ptr', 0)
EndFunc
Func OnAutoItExit()
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)
EndFunc