-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_TextInputCollector.ahk
67 lines (58 loc) · 1.67 KB
/
class_TextInputCollector.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @file
* @copyright Dedicated to Public Domain. See UNLICENSE.txt for details
*/
/**
* Collects text input
*
* Collected text accessible through Text property of the class.
*
* @note All `[:cntrl:]` characters (except new line) are ignored
*
* @code{.ahk}
#include <TextInputCollector> ; Assuming TextInputCollector.ahk is in your Lib folder
;Create new input collector and start it
inputCollector := new TextInputCollector()
inputCollector.start()
;Win+Shift+i - display collected text input
#+i::MsgBox % inputCollector.Text
* @endcode
*/
class TextInputCollector {
__New() {
;L0 - Disable hook's internal text buffer (we'll collect useful text in OnChar callback)
;I - set MinSendLevel to 1, i.e. ignores artificial input produced by AutoHotkey (which defaults to 0 SendLevel)
this.m_textInputHook := InputHook("L0 I")
this.m_textInputHook.VisibleText := true ;Allow text characters to be propagated to consumers (do not block them)
this.m_textInputHook.OnChar := ObjBindMethod(this.base, "OnCharacterTyped", &this)
}
Text[]
{
get {
return this.m_textInputBuffer
}
set {
}
}
start() {
this.m_textInputHook.Start()
}
stop() {
this.m_textInputHook.Stop()
}
__Delete() {
this.stop()
}
;private:
OnCharacterTyped(instanceAddress, hook, char) {
this := object(instanceAddress)
; OutputDebug % "char typed: ``" char . "`` (code: " asc(char) ")"
if (asc(char) = 10) { ; {Enter} key
this.m_textInputBuffer .= "`r`n"
} else if (char ~= "[^[:cntrl:]]") { ;All characters except control chars (see "POSIX character class" for more info)
this.m_textInputBuffer .= char
}
}
m_textInputHook := ""
m_textInputBuffer := ""
}