Jump to content

[Closed] How to Create an InputKey() Function?


Zohar
 Share

Recommended Posts

Hello

I need a function that when called, will wait for a Single Key from the user.

The Key can be a letter or number.

What I thought to do is this:

1) Show a small Window, with no controls on it

2) Wait for a KeyPress

3) When getting a KeyPress, Close the Window, and Return the Pressed Key

But after I do GUICreate(), there's no event for KeyPress.

What would be th simplest way to do it?

Thank you

Edited by Zohar
Link to comment
Share on other sites

  • Moderators

Zohar,

What you are asking for is essentially a KeyLogger, which is one of the taboo subjects here - look here to see why.

However, as Jon does say that "Posts on how to check for a few keys being pressed" are reasonably acceptable, I would point you in the direction of GUICtrlRead and a blank Input control. ;)

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

  • Developers

Just use a the HotKeySet() or _IsPressed() to wait for this one key.

When you want to wait for any key you need to monitor all keystrokes and its a kinda keylogger... and we do not want that here...do we? ;)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi

If I wanted to create a KeyLogger, I think I would use _IsPressed() ;)

Or simply download one from the internet :evil:

What I need here is different.

I need to wait for a Single key from the user.

Also I need a small Window to show up.

And also I need to Wait for it, because it's a selection by the user. for that Reason IsPressed() is not good for me.

BTW I created it in .NET with a single line of code.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    this.Text=e.KeyChar.ToString();
}

But since AutoIt is not .NET, then I need to make it in AutoIt so it can communicate with the rest of my AutoIt Code.

Regarding the ideas you wrote:

Melba:

From what I see in the Help, GUICtrlRead has nothing to do with Keys being pressed.. It reads the content of a Control, not a KeyPress.

Jos:

Using HotKeySet() will not be good here, because it is not a HotKey.

The user has to make a Selection, by pressing 1 Key(Number or Letter).

I will not add a HotKeySet() for every letter and number.

If anyone has other ideas, please write.

Edited by Zohar
Link to comment
Share on other sites

  • Moderators

Zohar,

Melba:

From what I see in the Help, GUICtrlRead has nothing to do with Keys being pressed

Correct, but read my post again - GUICtrlRead is only part of what I suggested. :evil:

"click"

M23

P.S. And if you do "get" it, do NOT post it here! ;)

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

Hello

I need a function that when called, will wait for a Single Key from the user.

The Key can be a letter or number.

What I thought to do is this:

1) Show a small Window, with no controls on it

2) Wait for a KeyPress

3) When getting a KeyPress, Close the Window, and Return the Pressed Key

But after I do GUICreate(), there's no event for KeyPress.

What would be th simplest way to do it?

Thank you

_WinAPI_SetWindowsHookEx

http://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_SetWindowsHookEx.htm

صرح السماء كان هنا

 

Link to comment
Share on other sites

Correct, but read my post again - GUICtrlRead is only part of what I suggested. :evil:

OHH

Sorry, I now understand what you mean.

It will work.. but it will not behave as I want..

I do not want it to get into a textbox.. I simply want to get an event when a key is pressed in the window - a window with no controls..

and to return it as the function's result.. :/

Thanks tho

_WinAPI_SetWindowsHookEx

http://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_SetWindowsHookEx.htm

Hi Wolf ;)

Thank you for the idea,

but do I really need to Set a Hook to get a Key Input from within a window that I created?

If it was a third party application's window, I would understand..

but from mine too?

Maybe I should stick with my .NET implementation, and have it communicate with AutoIt?

:/

Edited by Zohar
Link to comment
Share on other sites

$WH_KEYBOARD

$WH_KEYBOARD - Installs a hook procedure that monitors keystroke messages

$dwThreadId _WinAPI_GetCurrentThreadId()

Specifies the identifier of the thread with which the hook procedure is to be associated.

If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread

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

     Global $Key
    GUICreate(" ", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
    $Input = GUICtrlCreateInput("", 10, 5, 300, 20)
    GUISetState()
    Local $hmod
    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD, DllCallbackGetPtr($hStub_KeyProc) _
    , 0,_WinAPI_GetCurrentThreadId())
    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
    WEnd


Func IS_PRESSED($lParam)
Return int(BitAND($lParam,int(0x40000000)))
EndFunc

Func IS_RELEASED($lParam)
Return Not int(BitAND($lParam,int(0x40000000)))
EndFunc

Func _KeyProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf

    if IS_PRESSED($lParam) Then _
    GUICtrlSetData($Input, "Pressed " & Chr($wParam))
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>Cleanup

صرح السماء كان هنا

 

Link to comment
Share on other sites

Dear wolf9228

I sincerely thank you for that.

Yet I must admit that I think I will stay with my .NET window.

It's amazing how a simple functionality, can turn into such a difficult process(relatively).

All I wanted is to give the user the ability to choose from many options, via pressing 1 key.

And it became a code that contains hooks, my god (:

oh..

If only AutoIt was in .NET.

Such wonderful things could've been created, without wasting excess energy..

And to remind you all, an implementation in .NET:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    this.Text=e.KeyChar.ToString();
}
Edited by Zohar
Link to comment
Share on other sites

.NET is not slow.

It's slower than native code. Startup times for .NET applications is absurd. Giving up runtime performance to make compile-time code writing easier is just asinine at best and utterly stupid when any half-baked fucktard can sit down and write something runnable but certainly not passable or functional.

And let me remind you AutoIt uses an Interpreter too..

Really? I didn't know that. Also, do you know what would happen if AutoIt were written in .NET? You'd have the same slow speed AutoIt has now on top of the performance penalties .NET imposes. Add in the fact that it would require several MB of runtimes (The .NET framework, obviously) and well... it would be a slow-as-fuck bloated piece of shit. That nobody would care about.
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...