| View previous topic :: View next topic |
| Author |
Message |
Stom2008 Guest
|
Posted: Thu Aug 21, 2008 8:53 am Post subject: Hotkey with regex |
|
|
Hello AHK Forum,
I like to know if it is possible to use a kind of regex together with hotkeys
I like to do the following.
I will type:
0001-123456789 and ahk should convert it to 000112345679
0001-987654321 -> 0001987654321
0102-121212121 -> 0102121212121
for all possible numbers I like to replace the - after the number is typed.
I think the regex should be:
([0-9]{4,})-([0-9]{9,})
But I did not know if I can do it in ahk and how?
Best Regards,
Marcus |
|
| Back to top |
|
 |
Hasso
Joined: 23 Mar 2005 Posts: 158 Location: Germany
|
Posted: Thu Aug 21, 2008 9:52 am Post subject: Re: Hotkey with regex |
|
|
| Stom2008 wrote: |
0001-123456789 and ahk should convert it to 000112345679 |
I think there is a typo as the "8" has disappeared in the converted number?
Your problem seems rather strange to me - why don't you simply type the number without the "-"??? _________________ Hasso
Programmers don't die, they GOSUB without RETURN |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Aug 21, 2008 9:56 am Post subject: |
|
|
...
Yes, this is a typo.
0001-123456789 and ahk should convert it to 0001123456789
I do not really type the number. The nuber is "typed" by barcode scanning.
And the scan returns the number with the "-"
In the system I need the number without the "-"
Best Regards,
Marcus |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1496
|
Posted: Thu Aug 21, 2008 12:30 pm Post subject: |
|
|
You can write a script that catches scanner-speed input and disables the [-] key for a split second after four numbers have been 'typed' really quickly. | Code: | Loop % troll := 10
Hotkey, % "~" SubStr(A_Index, 0), scan_handler, on
scan_handler:
troll := troll * ( A_TimeSincePriorHotkey < 20 ) + 1
Hotkey, -, scan_handler, % (troll > 2) ? "on" : "off"
return | untested _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
Razlin
Joined: 05 Nov 2007 Posts: 434 Location: canada
|
Posted: Thu Aug 21, 2008 12:36 pm Post subject: |
|
|
ummm why dont you just
| Code: |
mystring = "0000-123456789"
StringReplace, MyString, MyString, -, , |
replace the - with nothing.
no regex required. _________________ -=Raz=- |
|
| Back to top |
|
 |
stom2008 Guest
|
Posted: Thu Aug 21, 2008 12:59 pm Post subject: |
|
|
Thanks for your answers
@[VxE]
I tried the script but nothing happens if the number is typed
@Razlin
That was my first idea. But I have no idea how to become the "scanner-type-input" into a variable to replace the "-"
Best Regards,
Marcus |
|
| Back to top |
|
 |
Razlin
Joined: 05 Nov 2007 Posts: 434 Location: canada
|
Posted: Thu Aug 21, 2008 2:29 pm Post subject: |
|
|
how about a hotsting.
Replace - with nothing.
 _________________ -=Raz=- |
|
| Back to top |
|
 |
Razlin
Joined: 05 Nov 2007 Posts: 434 Location: canada
|
Posted: Thu Aug 21, 2008 2:55 pm Post subject: |
|
|
hotstring may work but I'm not sure.. but for your other question. you can receive input with
| Code: | | Input, Variable_name_here, L10 |
Variable_name_here will contain 10 chars
Change L10 to what ever limit you want,
you can then use
stringreplace on Variable_name_here
anyhow.. hope one of these 2 work for you. _________________ -=Raz=- |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1496
|
Posted: Thu Aug 21, 2008 9:06 pm Post subject: |
|
|
| stom2008 wrote: | @[VxE]
I tried the script but nothing happens if the number is typed |
Of course, nothing is supposed to happen when a human 'types' numbers. That example script is only supposed to show how AHK might watch numeric input and, if it is too fast, deactivate the [-] key.
btw: you never mentioned whether your scanner's input simulates the numpad keys or the regular number keys... could be important.
This example tries to detect speed-input and then selects the input, cuts it, replaces [-], and pastes result.
| Code: | #UseHook
Loop 10 ; turn the number keys into hotkeys
Hotkey, % "~" SubStr(A_Index, 0), ~-, on
~-:: ; the minus key is a hotkey too
troll++, troll *= A_TimeSincePriorHotkey < 21
If (Troll > 12) ; if 14 or more numbers or minus
{ ; signs have been entered with a delay under 21ms
clip := clipboardall ; save clipboard
clipboard := "" ; clear clipboard
SendInput +{left 14}^x ; cut previous 14 chars
clipwait, 1 ; wait for number to sow up
Stringreplace, clipboard, clipboard, -
SendInput ^v ; paste number without minus
clipboard := clip ; restore clipboard
clip := ""
}
return |
_________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
|