Is there any way to empty the input buffer held in InputHook.Input, so that the script could easily discard any non-pertinent input?
EDIT: Only way I have figured out so far would be
Code: Select all
Hook.Stop
Hook.Start
Code: Select all
Hook.Stop
Hook.Start
Code: Select all
InputHook.Prototype.DefineProp('Clear', {Call: InputHook_Clear})
InputHook_Clear(this) {
#Requires AutoHotkey v2.0-rc.2
pInputHook := ObjPtr(this)
static is64bit := (A_PtrSize = 8)
static offsetof_Buffer := is64bit ? 56 : 32
pBuffer := NumGet(pInputHook, offsetof_Buffer, 'Ptr')
#DllLoad 'ucrtbase.dll'
DllCall('ucrtbase\free', 'Ptr', pBuffer)
static offsetof_BufferLengthMax := is64bit ? 68 : 40
BufferLengthMax := NumGet(pInputHook, offsetof_BufferLengthMax, 'Int')
if !pBuffer := DllCall('ucrtbase\malloc', 'Ptr', (BufferLengthMax + 1) * 2)
throw MemoryError('Out of memory.')
NumPut('UChar', 0, pBuffer)
NumPut('Ptr', pBuffer, pInputHook, offsetof_Buffer)
}
ih := InputHook("L2")
ih.Start()
ToolTip 'type something'
ih.Wait()
MsgBox 'u typed: ' ih.Input
ih.Clear()
MsgBox 'and now its gone: ' ih.Input
Code: Select all
#Requires AutoHotkey =v2.0-rc.2
InputHook.Prototype.DefineProp 'Clear', {Call: this => (
NumPut('int', 0, ObjPtr(this), A_PtrSize = 8 ? 64 : 36), ""
)}
; Esc to end. Enter to shift Input to Entry.
ih := InputHook('', '{Esc}')
ih.Entry := '' ; Custom property
ih.KeyOpt('{Enter}', 'N')
ih.OnKeyDown := (ih, vk, sc) => (
ih.Entry := ih.Input,
ih.Clear()
)
ih.Start()
while ih.InProgress {
Sleep 100
ToolTip 'Input: ' ih.Input '`nEntry: ' ih.Entry
}
I think that a vague notion of costs and side-effects (that you probably can't even begin to define) is not a good reason to avoid using the only apparent solution to a problem. Why not just do it, and observe for any problems? It is not as "costly" as using unsupported, version-specific code, even if you understand the code.ThePeter wrote:but that seems to be a costly operation and may have all kinds of side effects.
After a bit of testing, the cost in terms of processing time indeed does not seem to be a problem. The side effects are real and I am very able to define them. Each time I call Stop, the actions defined in the OnEnd handler will be executed, which will not necessarily be what I want if the only thing I want to do is wiping the buffer. Resetting of the timer is another, but that's not relevant for my use case. Anyway, I can certainly work around that.lexikos wrote: ↑09 Dec 2022, 19:27I think that a vague notion of costs and side-effects (that you probably can't even begin to define) is not a good reason to avoid using the only apparent solution to a problem. Why not just do it, and observe for any problems? It is not as "costly" as using unsupported, version-specific code, even if you understand the code.
"may have all kinds of side effects" gives off a very different vibe, and your reply doesn't do much to dispel this.The side effects are real and I am very able to define them.
If you want OnEnd to take some action only if input ended for some reason other than Stop being called, all you need to do is check ih.EndReason. If there's some part of the ending code that needs to run even when you end it by calling Stop, you can just put it in a function and call it both from OnEnd and after calling Stop.Each time I call Stop, the actions defined in the OnEnd handler will be executed, which will not necessarily be what I want if the only thing I want to do is wiping the buffer.
I only mentioned the side effects that are clear to me and that I have control over. Otherwise, indeed, I have no knowledge of whether side effects are generated. Hence my words "may have". Generally, it is not good programming practice to apply a functionality that you do not want in order to achieve a desired side effect of such functionality.
You are giving me an astonishingly hard time for even asking.