Jump to content

How do I reroute a keypress?


Sori
 Share

Go to solution Solved by Sori,

Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

Sorimachi,

 

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorimachi,

welcome to AutoIt and the forum!

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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