Crazy Idea: erase typing Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ryder
Posts: 61
Joined: 28 Apr 2022, 18:49

Crazy Idea: erase typing

Post by Ryder » 04 Oct 2022, 11:43

Hi all,

I'm grappling with a situation where I have an application that I don't want people to type into a certain field.

I need them to use a barcode scanner (to enforce accuracy).

One thing I could do, which sounds a little nuts... is to watch the timing of the keystrokes, and if at any time the spacing between keystrokes is greater than 50ms, say, that AHK does a ctrl-a delete (select all, then delete)... continually erasing anything that is typed.

Note: I only want this code to be operating when a particular field has focus.

The barcode scanner will type much faster... thus all characters would be allowed (including the Carriage Return that would cause the field to lose focus, thus stopping the routine).

Basically, I'm in over my head on all of this... so what does this code look like?

Sorry to ask for such extreme hand holding.

R

ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: Crazy Idea: erase typing

Post by ahk7 » 04 Oct 2022, 12:26

Why not simply https://www.autohotkey.com/docs/commands/BlockInput.htm until the bar code scanner has done its job

Alternatively, have a look at example #2 https://www.autohotkey.com/docs/commands/_If.htm#ExWordDelete (#If ActiveControlIsOfClass("Edit")) and use that to block input for a-z 0-9 hotkeys and "block" input for the bar code scanner field

Ryder
Posts: 61
Joined: 28 Apr 2022, 18:49

Re: Crazy Idea: erase typing

Post by Ryder » 04 Oct 2022, 13:24

ahk7 wrote:
04 Oct 2022, 12:26
Why not simply https://www.autohotkey.com/docs/commands/BlockInput.htm until the bar code scanner has done its job

Alternatively, have a look at example #2 https://www.autohotkey.com/docs/commands/_If.htm#ExWordDelete (#If ActiveControlIsOfClass("Edit")) and use that to block input for a-z 0-9 hotkeys and "block" input for the bar code scanner field
I don't think that would work because, as I understand it, the mouse is also blocked... which means the user couldn't click away from the field... next, the barcode scanner is interpreted as a keyboard, so it would also be blocked.

R

RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Crazy Idea: erase typing

Post by RussF » 04 Oct 2022, 13:28

@ahk7, a typical scanner is just another HID device and to Windows, appears as another keyboard. You can attach multiple keyboards to a PC and Windows will accept input from all of them identically. AHK can not differentiate which keyboard (or HID) that input is coming from. From the BlockInput documentation:
This mode blocks all user inputs unconditionally. Specify one of the following words:
On: The user is prevented from interacting with the computer (mouse and keyboard input has no effect).
The BlockInput command will block the scanner as well as the keyboard because it can't tell the difference.

I've been thinking about a solution, but so far no joy.

Russ

ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: Crazy Idea: erase typing

Post by ahk7 » 04 Oct 2022, 16:37

In that case: perhaps https://github.com/evilC/AutoHotInterception
AutoHotInterception (AHI) allows you to execute AutoHotkey code in response to events from a specific keyboard or mouse...
Forum viewtopic.php?f=6&t=45307
(never used it myself)

Ryder
Posts: 61
Joined: 28 Apr 2022, 18:49

Re: Crazy Idea: erase typing

Post by Ryder » 04 Oct 2022, 17:42

Solved in essentially the exact way I described:

I reset the Global variable OldTime whenever the field is activated... the rest is self explanatory.
Setting the CheckNum boolean tells the function if it's supposed to be working or not.

I also need to add the numpad keys... but this does the trick.

Code: Select all

~1:: NumKeyPress()
~2:: NumKeyPress()
~3:: NumKeyPress()
~4:: NumKeyPress()
~5:: NumKeyPress()
~6:: NumKeyPress()
~7:: NumKeyPress()
~8:: NumKeyPress()
~9:: NumKeyPress()
~0:: NumKeyPress()

NumKeyPress(){
   Global OldTime					;so the function knows to use the global variable
   Global CheckNum					;""
	
   If CheckNum{
	DeltaTime = 0
	If (OldTime = 0){				;for first character, just grab a tick
		OldTime := A_TickCount			;only good for systems on for < 49.7 days!!!??
	} 
	Else{						;If this isn't the first press
		NowTime := A_TickCount			;Get now
		DeltaTime := NowTime - OldTime		;compare to previous press
		OldTime := NowTime			;Remember the now time as the old
		If (DeltaTime > 100) {			;make sure it's less than .12 seconds
			Send ^a				;if not... select all
			Send {del}			;and delete it..
			OldTime := 0			;then reset the process
		}
	}
}


Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Crazy Idea: erase typing

Post by Lepes » 05 Oct 2022, 06:04

Now that the problem is solved I would like to add something: "Do a program fool safe and no one will be so fool to use your program". It is a non-written programming rule.

When you introduce a new technology (bar scanner) you need to provide a secondary solution: barcode scanneer don't recognize a bar code, dirty on barcode paper, reflections on the plastic makes barcode scanner fail, etc. How will handle this situation your people? Easy: stop the line, ask for another product that has a cleaner barcode and start again. So, you must provide a way to type a barcode by hand, bottom line.

Since you want everybody uses the barcode scanner, place a button that makes posible to introduce by hand but disable it when he press Enter or barcode has been recognized, and makes default option to be scanned.

In my honest opinion, supervisor is the one to encourage people to use barcode scanner, not a programmer.

Ryder
Posts: 61
Joined: 28 Apr 2022, 18:49

Re: Crazy Idea: erase typing

Post by Ryder » 05 Oct 2022, 10:22

Lepes wrote:
05 Oct 2022, 06:04
Now that the problem is solved I would like to add something: "Do a program fool safe and no one will be so fool to use your program". It is a non-written programming rule.

When you introduce a new technology (bar scanner) you need to provide a secondary solution: barcode scanneer don't recognize a bar code, dirty on barcode paper, reflections on the plastic makes barcode scanner fail, etc. How will handle this situation your people? Easy: stop the line, ask for another product that has a cleaner barcode and start again. So, you must provide a way to type a barcode by hand, bottom line.

Since you want everybody uses the barcode scanner, place a button that makes posible to introduce by hand but disable it when he press Enter or barcode has been recognized, and makes default option to be scanned.

In my honest opinion, supervisor is the one to encourage people to use barcode scanner, not a programmer.
Thank you for your comments.
What do you do when there is no supervisor? What do you do when the operators do not speak or read the language of the organization? And finally, what do you do when the need is much stronger than "encourage", but is in fact "absolute requirement"?

But to your point, the code includes a "secret" key press combination that disables the system if needed.

Ryder
Posts: 61
Joined: 28 Apr 2022, 18:49

Re: Crazy Idea: erase typing  Topic is solved

Post by Ryder » 05 Oct 2022, 11:40

Here is an updated/better solution:

Code: Select all

~1:: NumKeyPress()
~2:: NumKeyPress()
~3:: NumKeyPress()
~4:: NumKeyPress()
~5:: NumKeyPress()
~6:: NumKeyPress()
~7:: NumKeyPress()
~8:: NumKeyPress()
~9:: NumKeyPress()
~0:: NumKeyPress()

~Numpad1:: NumKeyPress()
~Numpad2:: NumKeyPress()
~Numpad3:: NumKeyPress()
~Numpad4:: NumKeyPress()
~Numpad5:: NumKeyPress()
~Numpad6:: NumKeyPress()
~Numpad7:: NumKeyPress()
~Numpad8:: NumKeyPress()
~Numpad9:: NumKeyPress()
~Numpad0:: NumKeyPress()

CheckNum := True

NumKeyPress(){
   Global CheckNum					;Use the Global boolean flag
	
   If CheckNum{						;Only perform the check if the flag is set True
	SetTimer, KillContents, 50		;Kill the contents in 50ms
   }
Return
KillContents:
	Send ^a							;if the timer expires select all
	Send {del}						;and delete it..
	SetTimer, KillContents, off		;Kill the timer
	Return
}
This requires that every numeric keystroke must occur within 50ms of the previous, or a timer will destroy the contents of the field. This is a far more aggressive approach which removes doing maths or the use of the tick counter.

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Crazy Idea: erase typing

Post by Lepes » 05 Oct 2022, 13:53

Ryder wrote:
05 Oct 2022, 10:22
Thank you for your comments.
What do you do when there is no supervisor? What do you do when the operators do not speak or read the language of the organization? And finally, what do you do when the need is much stronger than "encourage", but is in fact "absolute requirement"?

But to your point, the code includes a "secret" key press combination that disables the system if needed.
Ok, I understand. You kept in mind everything. I would suggest another Shorcut to disable once the barcode scanner. If someone catch the global one, you are in the same position.

The solution is to "touch their pocket": people that not use bar code scanner will be economically punished. There isn't mind weather it is true or not, the important thing is they believe its true :D

Post Reply

Return to “Ask for Help (v1)”