Handle Numpad Enter and Enter - Count characters

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 09:24

1)
Trying to handle Numpad Enter and Enter
When one of these buttons is pressed, the focus should be shifted to the desired location.
eg. (in the example)
from Text1 -> Text2
from Text2 -> ButtonReady

I've tried with
GroupAdd hkgroup, %WinName% to use with #IfWinActive ahk_group hkgroup
And it seems to work between editable fields, but only sometimes between the edit field Text2 and a button ButtReady.

My desire is (as example) .: if focus is on Text2 and the Enter is pressed the focus is set on the button, and if the Enter is pressed again the button ButtReady is pressed.

I have a small test program for this
Counting Characters!
(excuse me for some Swedish comments)

2) The next challenge is to count all characters and when the maximum number of characters is reached (15 pcs in the example) no more characters can be entered.
It is possible?
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 10:01

This is just for 1)

My desire is (as example) .: if focus is on Text2 and the Enter is pressed the focus is set on the button, and if the Enter is pressed again the button ButtReady is pressed.

Code: Select all

#SingleInstance force
#Persistent
Gui 1: Font, cgray s12 Normal, Arial
Gui 1: Add, text, x20 y20, Information .:

Gui 1: Font, cBlue s22 Normal, Arial
Gui 1: Add, Edit, x30 y+0 h35 w260 vText1 
Gui 1: Add, Edit, x30 y+2 h35 w260 vText2 

Gui 1: Font, s18 Bold, Arial
Gui 1: Add, Button, x100 y130 h40 w100 center vButtReady gtest, Ready!

Gui 1: Show, x100 y100 w320 h200, SomeRandomTitle



Return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp

Test:
	soundbeep, 500
	return

#IfWinActive SomeRandomTitle
~NumpadEnter::
~Enter::
	GuiControl, 1:-Default, ButtReady
	GuiControlGet FocusOn, FocusV	
	If (FocusOn = "Text1"){
		GuiControl 1: Focus, Text2		
	}else If (FocusOn = "Text2"){
		GuiControl 1: Focus, ButtReady
	}else If (FocusOn = "ButtReady"){
		GuiControl, 1:+Default, ButtReady
	}
Return
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 10:29

Thanks (again)!
Now even change focus works for me :bravo: .

Do you have any idea how the character input can be stopped? (in a good way?)
First I tested the option with limit Count characters - GUI Edit
(But got stuck in counting the characters right)
One way is to disable edit fields (but then you can't delete characters)

It would have been best to be able to count all characters before the last character appears on the screen and then stop the input.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 10:45

Albireo wrote:
11 Mar 2020, 10:29

Do you have any idea how the character input can be stopped? (in a good way?)
I'm pretty sure I could come up with something, but I'm tired (the fog is starting to roll over my brain) and am heading to bed shortly.

Perhaps later (if no one else has helped you by then) i'll take a stab at it.
Just so I'm clear, you want to keep the total (between edit1 and edit2) to be maxed at 15 chars right?
If so, why?
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 15:14

OK! (I like your solutions :-) )

Background
I am writing a program to help users to send SMS.
At first I thought that the user would write more text in the message.
But couldn't solve the problem of counting characters - (in the link I sent)
Was hoping to get a dynamic Limit in Gui Edit (but it doesn't seem to work)
The limit is 160 characters for one SMS, and 153x2 characters for two SMS and so on.

Then the criteria were changed, the users should write less text (and more text should automatically be generated as head and foot in the SMS)
Thought it would be easier to manage multiple Edit fields - without new lines (maybe 4-5 fields - not only two as in the example)

My desire
I'll try to explain my desire first.

First an SMS head, then EditField1 , EditField2 , EditField3 , EditField4
At last the SMS ends with a standard text.
(Only EditFields with characters should be copied in to the SMS-text).

eg.
If only one EditField has characters, the SMS result should be:
First an SMS head, then EditField1 At last the SMS ends with a standard text.

If two EditField has characters, the SMS result should be:
First an SMS head, then EditField1 , EditField2 At last the SMS ends with a standard text.

One way, to solution?
I'm wondering if this could be a solution for me. ( but maybe a little difficult to explain. )
Right now I have no test program with this strategy.

As an example .:
To the left in the GUI the user have the EditFields.
To the right the result SMS is shown (with the result text from EditField included)
The characters (with CR) is counted in the result field.

When the result text has reached the limit, nothing more happens to that text (even if the user continues to write)
Maybe this would work?
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Handle Numpad Enter and Enter - Count characters

11 Mar 2020, 16:43

Hellbent wrote:
11 Mar 2020, 10:01
This is just for 1)...
I got trouble to using .: #IfWinActive SomeRandomTitle A window name works in this example, but I want to use an variable instead like this .: #IfWinActive %WinName% (But it doesn't work.)

I have even tried .:

Code: Select all

	WinName = SomeRandomTitle
	...
	Gui 1: Show, x100 y100 w600 h200, %WinName%
	WinGet WinID,, %WinName%
	Return
	...
	#If WinActive("ahk_id " WinID)
(or #IfWinActive("ahk_id " WinID) does not work either.)

Code: Select all

WinName = Counting Characters!
GroupAdd hkgroup, %WinName%
...
#IfWinActive ahk_group hkgroup
Works! (but not for the Button)
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Handle Numpad Enter and Enter - Count characters

12 Mar 2020, 07:44

Wrote a solution / example to handle the edit fields as my desire above. (and I think it works)
- Increased the number of edit fields to four.
- The number of characters remaining is displayed below the message result.

However, failed to handle Enter and the GUI button (with a window name as an variable)
So this desire have I ignore.

What I still found tricky to deal with, was counting characters and providing informative messages if the limit of maximum number characters being exceeded.
Another thing that seems difficult to solve is the pling sound along with the GUI.
sometimes no pling is heard, sometimes the pling is cuted, and of course, sometimes the pling sounds as desired

This is what the test code looks like now .:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#Persistent

WinName = Counting Characters!
GroupAdd hkgroup, %WinName%	; To use with #IfWinActive ahk_group hkgroup

MaxChar = 60	; 47(Head+Foot) + 13 free characters
MessHead := "Hello!`nSome text "
MessFoot := "`nThe end of the message`nRegards"
LenChar := StrLen(MessHead) + StrLen(MessFoot)

; GUI handle
	Gui 1: Font, cgray s12 Normal, Arial
	Gui 1: Add, text, x20 y20, Information .:

	Gui 1: Font, cBlue s22 Normal, Arial
	Gui 1: Add, Edit, x30 y+0 h35 w260 vText1 gGetCount
	Gui 1: Add, Edit, x30 y+2 h35 w260 vText2 gGetCount
	Gui 1: Add, Edit, x30 y+2 h35 w260 vText3 gGetCount
	Gui 1: Add, Edit, x30 y+2 h35 w260 vText4 gGetCount

	Gui 1: Font, cGreen s16 Normal, Arial
	Gui 1: Add, text, x400 y20 h150 w300 vResult, %MessHead% %MessFoot%
	Gui 1: Font, cGreen s14 Normal, Arial
	Gui 1: Add, text, x400 y200 h20 w300 vCount, % "Characters left .: " MaxChar - LenChar
	Gui 1: Font, cred s14 Bold, Arial
	Gui 1: Add, text, x400 y220 h20 w300 vCountMess hidden, % "No characters left"

	Gui 1: Font, s18 Bold, Arial
	Gui 1: Add, Button, x100 y200 h40 w100 center vButtNext gReady, Ready!

	Gui 1: Show, x100 y100 w700 h250, %WinName%
	; WinGet WinID,, %WinName%
	; MsgBox % WinID
Return



; #IfWinActive(WinName)
; #IfWinActive("ahk_id " WinID)
#IfWinActive ahk_group hkgroup
~NumpadEnter::
~Enter::
	GuiControlGet FocusOn, FocusV	; Kontrollera var Focus finns
	Loop 3
	{	If ( FocusOn = "Text" A_Index )
		{	NextField := A_Index + 1 
			GuiControl 1: Focus, Text%NextField% 		; Sätt fokus på nästa fält
			Break
		}
	}
Return


GetCount:
	Gui 1: Submit, NoHide
	RowCount = 0
	Message := MessHead
	Loop 4	; Number of EditFields
	{	GuiControlGet InpText%A_Index%, 1:, Text%A_Index%	; Catch the text that is being written in each editfields
		If (StrLen(Text%A_Index%) > 0)
		{	RowCount += 1
			If (RowCount = 1)
				Message .= Text%A_Index%
			else
			{	; If a new edit field has been entered, a comma will also be added (two characters)
				If ( MaxChar - StrLen(Message) > 2 )
					Message .= "," Text%A_Index%
				else	; Message too long	
				{	GuiControl 1: Show, CountMess	; Maximum number of characters
					SoundBeep 500, 500
				}
			}
		}
	}
	Message .= MessFoot
	
	CharCount := MaxChar - StrLen(Message)
	GuiControl 1: , Count, % "Characters left .: " CharCount
	If (CharCount >= 0)
	{	GuiControl 1: , Result, % Message
		Gui 1: Font, s16 cGreen, Normal, Arial
		GuiControl 1: Font, Count
		If(CharCount > 0)
			GuiControl 1: Hide, CountMess
		else
			GuiControl 1: Show, CountMess		; Maximum number of characters
	}
	else
	{	Gui 1: Font, s16 cRed, Bold, Arial
		GuiControl 1: Font, Count
		GuiControl 1: Show, CountMess	; Maximum number of characters
		SoundBeep 500, 500
	}
	

Return


ESC::
EndProg:
GuiClose:
GuiEscape:
	MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%,
	( LTrim
		ESC is pressed! or `n
		Windows is closed! `n`n
		This program will be terminated!
	)
	MsgBox ,,, Program is terminated!, 1
	ExitApp
Return

Ready:
	MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, Text1 .: %Text1% `nText2 .: %Text2% `nCharCount .: %CharCount%
	ExitApp
Return
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Handle Numpad Enter and Enter - Count characters

12 Mar 2020, 11:33

Albireo wrote:
12 Mar 2020, 07:44
However, failed to handle Enter and the GUI button (with a window name as an variable)

Code: Select all

#SingleInstance force

WinTitle := "SomeRandomTitle"

Gui,1:+hwndGuiHwnd
Gui 1: Font, cgray s12 Normal, Arial
Gui 1: Add, text, x20 y20, Information .:
Gui 1: Font, cBlue s22 Normal, Arial
Gui 1: Add, Edit, x30 y+0 h35 w260 vText1 
Gui 1: Add, Edit, x30 y+2 h35 w260 vText2 
Gui 1: Font, s18 Bold, Arial
Gui 1: Add, Button, x100 y130 h40 w100 center vButtReady gtest, Ready!
Gui 1: Show, x100 y100 w320 h200, % Wintitle

Return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp

Test:
	soundbeep, 500
	return

;~ #If WinActive( Wintitle )  		;<------------------ Works
#If WinActive( "ahk_id " GuiHwnd ) 	;<------------------ Works

~NumpadEnter::
~Enter::
	GuiControl, 1:-Default, ButtReady
	GuiControlGet FocusOn, FocusV	
	If (FocusOn = "Text1"){
		GuiControl 1: Focus, Text2		
	}else If (FocusOn = "Text2"){
		GuiControl 1: Focus, ButtReady
	}else If (FocusOn = "ButtReady"){
		GuiControl, 1:+Default, ButtReady
	}
	Return
	
#If
I have never used GroupAdd, I also don't really see myself using it in the future, so you are on your own with that.
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Handle Numpad Enter and Enter - Count characters

12 Mar 2020, 12:48

Thanks!
Now the GUI also works with a variable name - and press the Buton with Enter
(this feels like a better solution than Group Add)

(But - I do not hear any sound when the button is pressed)
What does #if on the last line mean?
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Handle Numpad Enter and Enter - Count characters

12 Mar 2020, 13:19

Albireo wrote:
12 Mar 2020, 12:48
What does #if on the last line mean?
That just exits the #If so that you can add other hotkeys without #If WinActive() blocking them.

here is a draft of 1) and 2).
Let me know if it works for you.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
Gui, 1:+AlwaysOnTop hwndGuiHwnd
Loop 4	
	Gui, 1:Add, Edit, xm w300 r1 limit15 vEdit%A_Index% gSubmitValues, 
Gui, 1:Add, Button, xm w200 r1 vButt1 gTest, Button
Controls := ["Edit1","Edit2","Edit3","Edit4","Butt1"]
Gui, 1:Add, Text, xm w300 r2 vtext1,Total Length = 0`nmessage = 
Gui, 1:Show, w400 h300, % WinTitle := "SomeRandomName"
bc := 0
return
GuiClose:
GuiContextMenu:
*Esc::
	ExitApp
	
Test:
	if(++bc=2){
		bc := 0
		loop, 4
			GuiControl 1:, edit%A_Index%, % edit%A_Index% := ""
		GuiControl 1: Focus, edit1
		return
	}
	SoundBeep, 500
	return

SubmitValues:
	Gui, 1:Submit, NoHide
	totalKeys := "" , message := ""
	Loop, 4	
		totalKeys += strlen( Edit%A_Index% )
	loop 4	
		GuiControl, % "1:+Limit" 15 - ( totalKeys - strlen( Edit%A_Index% ) ), Edit%A_Index%
	if(totalKeys>=16){
		GuiControl, 1:,% A_GuiControl, % %A_GuiControl% := SubStr( %A_GuiControl%, 1, strlen( %A_GuiControl% ) - ( totalKeys - 15 ) )
		loop 4	
			GuiControl, % "1: +Limit" strlen( Edit%A_Index% ), Edit%A_Index%
		SoundPlay,*48
		totalKeys := 15
	}
	Loop, 4	
		message .= Edit%A_Index%
	GuiControl, 1:,Text1, % "Total Length = " totalKeys "`nmessage = " message
	return

#If WinActive( "ahk_id " GuiHwnd ) 
~NumpadEnter::
~Enter::
	GuiControl, 1:-Default, Butt1
	GuiControlGet FocusOn, FocusV	
	loop, % Controls.Length(){
		if(FocusOn = Controls[Controls.Length()]){
			GuiControl, 1:+Default, Butt1
			return
		}else if(FocusOn = Controls[A_index]){
			GuiControl 1: Focus, % Controls[A_Index+1]
			return
		}
	}
	Return	
#If


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy and 386 guests