Jump to content

Handle Key Presses in Input Control


DickWms
 Share

Recommended Posts

I'm trying to catch non printing characters (like Delete, F5, Shifte F5, etc) when a user is typing into an Input Control. The sample code below doesn't work (the Input MsgBox only appears when the enter key is pressed and the Key Pressed MsgBox never appears). It appears to me this is because the Input Control doesn't send a GUI message every time a key stroke is received.

So my question is how can I capture individual key presses, including non printing ones, to an Input Control? Or should I be using a different control?

Thank you - Dick williams

#include
#include

$window = GUICreate("Test", 200, 100)
$inputBox = GUICtrlCreateInput ( "Input Box", 50, 50 , 100, 20)
GUISetState()
While 1
$msg = GUIGetMsg()
Select

Case $msg = $inputBox
MsgBox(0, "Input Box", "Case Executed")
If _IsPressed(75) Then
MsgBox(0, "Key Pressed", "F5")
EndIf

Case $msg = $GUI_EVENT_CLOSE
Exit

EndSelect
WEnd
Link to comment
Share on other sites

  • Moderators

DickWms,

how can I capture individual key presses, including non printing ones, to an Input Control?

Surely an input control is for inputting text - so why do you want to capture other keypresses as well? :huh:

M23

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

  • Moderators

DickWms,

If all you are looking for are those few keys then why not use _IsPressed in your idle loop? :)

M23

Edit:

Here is how you could look for the "Delete" key to empty the input:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
    EndSwitch

    If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput) And _IsPressed("2E", $hDLL) Then
        GUICtrlSetData($cInput, "")
    EndIf

WEnd

Any help? :)

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

Yes - that is a great help and seems to be working fine.

One more question - the application I'm writing uses a tab control and the input control is on tab0. I just wanted the Delete Key shortcut to work when tab0 is active. So I added a clause to your If statement and made it look like this:

If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput) And _IsPressed("2E", $hDLL) And GUICtrlRead($tab) = 0

This doesn't work as I expected. If I put text in the input control, click on another tab, hit the Delete key, and then return to tab0, the input contol will be blank.

Dick Williams

Link to comment
Share on other sites

  • Moderators

DickWms,

A peculiarity of Windows is causing this. When you switch to another tab the whole content of the input is selected while the input also retains the keyboard focus - so pressing "Delete" still affects the input and replaces all content. You can see that quite clearly in this short script - just put some text into the input and then swap tabs. Whan you return to Tab 0 you will see that all the text in the input is highlighted (highlit?): :(

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)
$cTab = GUICtrlCreateTab(10, 10, 480, 380)
$cTab_0 = GUICtrlCreateTabItem("Tab 0")
$cInput = GUICtrlCreateInput("", 50, 50, 200, 20)
$cTab_1 = GUICtrlCreateTabItem("Tab 1")
GUICtrlCreateTabItem("")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

The trick is to reset the focus to another control when you change tabs - like this: ;)

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)

$cTab = GUICtrlCreateTab(10, 10, 480, 380)
$cTab_0 = GUICtrlCreateTabItem("Tab 0")
$cInput = GUICtrlCreateInput("", 50, 50, 200, 20)
$cTab_1 = GUICtrlCreateTabItem("Tab 1")
GUICtrlCreateTabItem("")

$cButton = GUICtrlCreateButton("Test", 10, 450, 80, 30) ; <<<<< Just for the demo - you could use any control

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
        Case $cTab ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< When tab is actioned
            GUICtrlSetState($cButton, $GUI_FOCUS) ; <<<<<<<<<<<<<< Change the focus
    EndSwitch

    If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput) And _IsPressed("2E", $hDLL) And GUICtrlRead($cTab) = 0 Then
        GUICtrlSetData($cInput, "")
        While _IsPressed("2E", $hDLL) ; <<<<<<<<<<<<<<<<<<<<<<<<<<< Not really necessary, but just wait until the key is released
            Sleep(10)
        WEnd
    EndIf

WEnd

All clear? :)

M23

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

  • Moderators

DickWms,

Glad I could be of help. :)

M23

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

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

×
×
  • Create New...