Hook keyboard replace Ucase/Lcase characters [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Hook keyboard replace Ucase/Lcase characters [Resolved]

Post by Kwai chang caine »

Hello at all

I would like replace the UCASE letter F by the Lcase letter g
And also the opposite : Replace the LCASE letter d by the Ucase letter H
And impossible, for the moment if the first letter is UCASE, the letter who replace it, is also UCASE, and the opposite have the same behavior :|

If someone know how do this exploit :oops:

Code: Select all

#WH_KEYBOARD_LL = 13
#INPUT_KEYBOARD = 1
#KEYEVENTF_SCANCODE = $8

Global myKeyHook, SearchCharacter.s, ReplaceCharacter.s
Global Dim Key.s(256)

Structure INPUT_DATA

 type.l

 StructureUnion
  mi.MOUSEINPUT
  ki.KEYBDINPUT
  hi.HARDWAREINPUT
 EndStructureUnion

EndStructure

Procedure.l KeyProc(nCode.l,wParam.l,lParam.l)

 Protected *keyInput.KBDLLHOOKSTRUCT
 Protected InputData.INPUT_DATA
 
 If nCode = #HC_ACTION
    
  *keyInput = lParam  
  GetKeyboardState_(@Key())
  ToAscii_(*keyInput\vkCode, *keyInput\scanCode, @Key(), @RealCharacter_Ascii.w, 0)
  
  ; Key pressed
  RealCharacter_VkCode = *keyInput\vkCode
  RealCharacter_State = 0 ; I don't know how obtain the STATE of the HOOK :-(
  RealCharacter_ScanCode = *keyInput\scanCode
  RealCharacter.s = Chr(RealCharacter_Ascii)
  
  ; Character to replace
  SearchCharacter_VkCode = VkKeyScan_(Asc(SearchCharacter)) & $FF
  SearchCharacter_State = VkKeyScan_(Asc(SearchCharacter)) >> 8
  SearchCharacter_ScanCode = OemKeyScan_(Asc(SearchCharacter))
  SearchCharacter_Ascii = OemKeyScan_(Asc(SearchCharacter))
  
  ; Replacer character 
  ReplaceCharacter_VkCode = VkKeyScan_(Asc(ReplaceCharacter)) & $FF
  ReplaceCharacter_State = VkKeyScan_(Asc(ReplaceCharacter)) >> 8
  ReplaceCharacter_ScanCode = OemKeyScan_(Asc(ReplaceCharacter))
      
  If RealCharacter_VkCode = SearchCharacter_VkCode
        
   Inputdata\type = #INPUT_KEYBOARD
   inputdata\ki\wVk = ReplaceCharacter_VkCode
   inputdata\ki\wScan = ReplaceCharacter_ScanCode
  
   If inputdata\ki\wVk <> 0
    
    inputdata\ki\time = *keyInput\time
    InputData\ki\dwExtraInfo = *keyInput\dwExtraInfo     
    
    If wParam = #WM_KEYUP
     InputData\ki\dwFlags = #KEYEVENTF_SCANCODE|#KEYEVENTF_KEYUP
    EndIf
      
    SendInput_(1, InputData, SizeOf(INPUT_DATA))
    Result = #True
    
    ; Clear the buffers
    RtlZeroMemory_(InputData,SizeOf(INPUT_DATA))
    RtlZeroMemory_(*keyInput,SizeOf(KBDLLHOOKSTRUCT))
        
   Else
   
    Result = CallNextHookEx_(myKeyhook, nCode, wParam, lParam)
    
   EndIf
   
  Else
 
   Result = CallNextHookEx_(myKeyhook, nCode, wParam, lParam) 
   
  EndIf 
  
 Else
 
  Result = CallNextHookEx_(myKeyhook, nCode, wParam, lParam)
  
 EndIf
 
 ProcedureReturn Result
 
EndProcedure

; First try
SearchCharacter = "F"
ReplaceCharacter = "g"

; ; Second try
; SearchCharacter = "d"
; ReplaceCharacter = "H"

hInstance = GetModuleHandle_(0)

If hInstance
 myKeyhook = SetWindowsHookEx_(#WH_KEYBOARD_LL, @KeyProc(),hInstance,0)
Else
 MessageRequester("hook", "can't get module handle")
EndIf

OpenWindow(0, 0, 0, 350, 100, "")
Repeat : WaitWindowEvent(100) :Until GetAsyncKeyState_(#VK_ESCAPE)
Have a good day
Last edited by Kwai chang caine on Wed Jan 25, 2023 10:55 am, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Hook keyboard replace Ucase/Lcase characters

Post by infratec »

You have to handle the shift key too.
You need a sequence of key messages wivh also press and release or release and press the shift key.

But why you don't load the corrrect keyboard mapping?
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Hook keyboard replace Ucase/Lcase characters

Post by Kwai chang caine »

Thanks INFRATEC for your quick answer 8)

Aaaaah !! i believed the SHIFT/TAB/CTRL is in the key :oops:
Like when you have show to me with your nice code

Code: Select all

VkKeyScanValue = VkKeyScan_('A')
Debug "VkKeyScan_() = " +  "State: " + Str(VkKeyScanValue >> 8) + " Key: " + Str(VkKeyScanValue & $FF)
VkKeyScanValue = VkKeyScan_('a')
Debug "VkKeyScan_() = " +  "State: " + Str(VkKeyScanValue >> 8) + " Key: " + Str(VkKeyScanValue & $FF)
Master wrote:But why you don't load the corrrect keyboard mapping?
Because i have buy a very little chinese PC GPD, nearly like the size of the hand :shock:

Image

I searched a very little PC in notebook format, for replace my old EEEPC of ASUS under XP :shock: after nearly 20 years of job 8)
And impossible to find a NoteBook format, now just exist the tablet :evil:

So i'm forcing to buy chinese GPD, the only one who build this old format, splendid jewel, but not exist French version AZERTY

The chineses says : "The french are like the cheese in their food...
Not really important customer base !!!" :mrgreen: :lol:


Then i have modified W10 for have the French keyboard, and that works perfectly
But the letters writing on the Keyboard is always QWERTY :|
And numerous other keys not writing the letter writting on the key, for example , ? ; / etc ...
It's impossible to use it like this, furthermore the letter M not exist on the keyboard :shock:
I'm forced to do FONCTION+] for have "m" and "SHIFT + FONCTION + ]" for have "M", believe me..it's a real mess :twisted:

At the begining i also buy AZERTY stickers :idea:

Image

For the letters, that works, but for ,?;.:/!§, etc .. i must cut in microscopic piece of each stickers and for a new splendid PC it's really horrible :?
Furthermore this little PC have all of the bigs, he have even keyboard luminous :shock:
And with my stickers...goodbye the light, for one time i have this nice function :|

So, i have try after software who modify BDR or others Windows parameters, but it's when even sometime dangerous, a day i have lost my Q...replaced by B, modified in BDR, impossible to go back :oops:

Then i say to me : "PureBasic is your friend" and have searched/finded a magical code of you and another of RASHAD
After try this jewels i have again one time see that
only him, and his MASTERS of the bits, can save little KCC of this hell 8)

Image

And it's the begining of this travel of KCC in the HellKeyboardLand during since 6 days already :mrgreen:
Last edited by Kwai chang caine on Wed Jan 25, 2023 10:47 am, edited 2 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Hook keyboard replace Ucase/Lcase characters

Post by Kwai chang caine »

Thanks at you two, i not know this function of WINDOWS :shock:
I take a look for die less stupid :D (Very difficult mission for KCC :mrgreen: )

And i try when even to terminate my software, i have when even works on it several days...it's a little bit my children...like the thousands of others never terminate :lol:
But thanks for your tip..i have again a solution in the case i give up :wink:

Again thanks for your precious helps at all
I wish you a very good day 8)
ImageThe happiness is a road...
Not a destination
Post Reply