Jump to content

How do I reroute a keypress?


Go to solution Solved by Sori,

Recommended Posts

Posted (edited)

I have tried 2 solutions, with 2 different issues.

 

---------------------------------------------------------

Attempt 1:

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

Dim $reRouteKeys = 1

Global $hHook, $hStub_KeyProc, $buffer = ""
Local $hmod

$hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
$hmod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

While 1
   If $reRouteKeys = 1 Then
      ToolTip("ReRoute: On", 5, 5)
   Else
      ToolTip("ReRoute: Off", 5, 5)
   EndIf
WEnd

Func EvaluateKey($keycode)
   If $keycode = 65 Then
      If $ReRouteKeys = 1 Then
         send("{1}")
      EndIf
   EndIf
EndFunc

;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    Else
        Local $flags = DllStructGetData($tKEYHOOKS, "flags")
        Switch $flags
            Case $LLKHF_ALTDOWN
                ConsoleWrite("$LLKHF_ALTDOWN" & @CRLF)
            Case $LLKHF_EXTENDED
                ConsoleWrite("$LLKHF_EXTENDED" & @CRLF)
            Case $LLKHF_INJECTED
                ConsoleWrite("$LLKHF_INJECTED" & @CRLF)
            Case $LLKHF_UP
                ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @CRLF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
 EndFunc
 

 

The problem: When I press "a" it sends "1a"

How do I prevent the passthrough?

---------------------------------------------------------

 

Attempt 2:

#Include <HotKey.au3>
#Include <vkConstants.au3>

Dim $reRouteKeys = 1

_HotKey_Assign($VK_A, 'Message')

; Assign "CTRL-ESC" with Quit()
_HotKey_Assign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')

While 1
   If $reRouteKeys = 1 Then
      ToolTip("ReRoute: On", 5, 5)
   Else
      ToolTip("ReRoute: Off", 5, 5)
   EndIf
WEnd

Func Message()
   If $reRouteKeys = 1 Then
      Send("{1}")
    Else 
      Send("{a}")
     EndIf
  EndFunc     ;==>Message

  Func   Quit()
      Exit
EndFunc     ;==>Quit
 

The problem: Send() does nothing.

If you place MsgBox(0, "Test", "Test") in the section, it will trigger the message box, so the function is being called, but send isn't working.

Edited by Melba23
Added code tags

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

  • Moderators
Posted (edited)

Sorimachi,

 

  Quote

That took me a bit to color code it all

If you use code tags - see here how to do it you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

M23

Edit: And welcome to the AutoIt forum, by the way. :)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Sorimachi,

welcome to AutoIt and the forum!

Can you please tell us why you need to reroute keypresses?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

The idea popped in my head for a context sensitive keyboard.

I don't know what I would do with it though :D

I figured I would get the coding then throw it on the backburner for future useage.

And it's also challenging me and I don't like to be challenged.

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

  • Solution
Posted

Using a mixture of ispressed and send() I got it all coded.

Thank you for trying to help.

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...