trouble with script I am trying to develop for Morse Code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

trouble with script I am trying to develop for Morse Code

Post by spexnut » 27 Nov 2014, 06:27

I am a complete beginner with AutoHotKey, ( For that matter, apart from a very little experience with BASIC, any programming language)

however I have completed the basic tutorials, viewed many YouTube videos about using AHK, and searched the various Forums for

information as I try to progress with this deceptively powerful language.
I am trying to write an AHK script, which will, when each key (a through to z) on a computer keyboard is pressed, the appropriate Morse Code

sound for the key is produced by the computer speaker, and display the key pressed in a MsgBox.

The relationship between Morse dots and dashes (Usually known as dits and dahs in Ham Radio) from the ITU Morse Code recommendations,

Dit = basic unit length
Dah = three Dits
Space between the elements forming the same letter = one Dit
Space between letters =three Dits
Space between words = seven Dits

I would prefer not to use MIDI or .wav files to produce the sounds

Based on the above information I have used the SoundBeep command as follows'
; For the letter "a"

Dit, SoundBeep, 1500, 200
Sleep, 80
Dah, SoundBeep, 1500, 600

and my code, so far is
;
;
;

Loop, {
Input, Key, L1 ; Read a key from the keyboard
if (Key = "a" ) ;Morse dit dah . -
SoundBeep, 1500, 200 ; Beep for a dit
Sleep, 80 ; delay for timing correction
SoundBeep, 1500, 600 ; Beep for a dah
break
}
MsgBox, % "The key was """ Key """."


Loop, {
Input, Key, L1
if (Key = "b" ) ;Morse dah dit dit -...
SoundBeep, 1500, 600
Sleep, 80
SoundBeep, 1500, 200
Sleep, 80
SoundBeep, 1500, 200
Sleep, 80
SoundBeep, 1500, 200
break
}
MsgBox, % "The key was """ Key """."

Loop, {
Input, Key, L1
if (Key = "c" ) ;Morse dah dit dah dit -.-.
SoundBeep, 1500, 600
Sleep, 80
SoundBeep, 1500, 200
Sleep, 80
SoundBeep, 1500, 600
Sleep, 80
SoundBeep, 1500, 200
break
}
MsgBox, % "The key was """ Key """."

return

ExitApp

;
;
;
Apart from the fact that the above script does not work properly, there must be a more efficient way of getting the correct result.
(It only works in the order of the list in the script, not randomly, and only once).
Can I create a variable for Dit and Dah which contain (Or reference in some way) the SoundBeep and Sleep values?

How do I do this? The more I read the Docs about variables, the more confused I become.
I am using SciTe4AutoHotkey, latest version, and QuickAHK for trying snippets.(Big Thanks to the Developers of these Tools), an a laptop

running Windows 8.1 64 bit, fully updated.
I am a great believer in Benjamin Franklin's "Tell me and I forget, teach me and I may remember, involve me and I learn" maxim, so will

somebody please help or point me in the right direction.
Thanks.

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 27 Nov 2014, 12:10

Here's one way this could be approached. I'll describe the process, instead of just writing the code, as per Benjamin Franklin's preference.

The general idea is this: Write a function that can be given any letter. Based on the letter the function receives, it will play the appropriate sequence of sounds and pauses.

basic function example:

Code: Select all

MyFunction("A")
MyFunction("B")
return

; Function Definition
; This function will take one parameter as input and display it
MyFunction(x) { 
    MsgBox, % "The input value is " x
    return
}
Next, the way the function will determine which sequence of dits, dahs and pauses to play is it will use an array.
Within the function, create an array. This array will use the letters as "Keys" which will each correspond to a "Value". (Key and Value have a special meaning here, see the page on arrays I linked to)
The value stored will be a string that represents the sequence of dits, dahs and pauses. Ex:

Code: Select all

MyArray := {A: ". -", C: "- . - ."}
Var := MyArray["C"]
MsgBox, % Var
Once the function looks up the letter in the array and finds the corresponding string, it will iterate over each character in the string one at a time using a Parsing Loop. Within the loop, it will use if, else if, else to determine whether to play a dit, dah or pause.

Hope it helps :)

There are other ways to do this. For example, instead of storing a string and using a parsing loop, the array could store simple arrays, where each value is a dit, dah or pause. Then it could use a for loop to get each value in order.

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: trouble with script I am trying to develop for Morse Cod

Post by garry » 27 Nov 2014, 13:43

thank you , Kon
just found this old GWBASIC program

Code: Select all

10 REM Morse code - MORSE.BAS
20 CLEAR :CLS :KEY OFF
30 PRINT "Type a message and see it appear as Morse Code." :PRINT
40 DIM CODE$(122)
50 RESTORE
60 FOR A = 32 TO 122
70   READ CODE$(A)
80   PRINT CHR$(A);"  ";
90 NEXT A
100 PRINT :PRINT
110 INPUT "Use any of the above - What is your message "; MESSAGE$
120 PRINT
130 L = LEN(MESSAGE$)
140 FOR B = 1 TO L
150   PRINT CODE$(ASC(MID$(MESSAGE$,B,1)));" ";
160 NEXT B
170 PRINT :PRINT
180 END
190 DATA " ","!","|","#","$","%","&","'","(",")",".","+",",","-","   ","/"
200 DATA -----,.----,..---,...--,....-,.....,-....,--...,---..,----.
210 DATA ":",";","<","=",">","?","@"
220 DATA .-, -..., -.-., -.., ., ..-., --., ...., .., .---, -.-, .-.., --
230 DATA -., ---, .--., --.-, .-., ..., -, ..-, ...-, .--, -..-, -.--, --..
240 DATA "[","\","]","^","_","'"
250 DATA ._, -..., -.-., -.., ., ..-., --., ...., .., .---, -.-, .-.., --
260 DATA -., ---, .--., --.-, .-., ..., -, ..-, ...-, .--, -..-, -.--, --..

RUN
Type a message and see it appear as Morse Code.

   !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /  0  1  2  3  4  5  6  7  8  9  :
  ;  <  =  >  ?  @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T
U  V  W  X  Y  Z  [  \  ]  ^  _  `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
  p  q  r  s  t  u  v  w  x  y  z

Use any of the above - What is your message ? ABCD SOS

.- -... -.-. -..   ... --- ...

Ok 

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 27 Nov 2014, 14:21

Thanks, Kon and Garry, I shall have to do some more reading and try a few examples

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 28 Nov 2014, 15:57

Hi, I need some more help please.
Can someone clarify a few things about Functions?
Is there a convention for naming a function? Can the name be anything at all, preferably something memorable/to indicate it's function ;)
Where should Functions be placed in the script?
How do I call the Function when it is needed?
Can SoundBeep, MsGBox and Sleep be used in the same Function? I don't think it is allowed to have more than one command on a line.
Most of the documentation/examples seem to be about using functions for arithmetic/math stuff.
Thanks.

Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: trouble with script I am trying to develop for Morse Cod

Post by Bkid » 28 Nov 2014, 16:16

spexnut wrote:Is there a convention for naming a function? Can the name be anything at all, preferably something memorable/to indicate it's function ;)
Generally you want to name it something that refers to what it does, or you might forget. ;)

spexnut wrote:Where should Functions be placed in the script?
I usually place them at the very bottom, to keep them out of the way. :p

spexnut wrote:How do I call the Function when it is needed?
functionname(x, y) (if x/y are used in the function)

spexnut wrote:Can SoundBeep, MsGBox and Sleep be used in the same Function? I don't think it is allowed to have more than one command on a line.
You should be able to use both of those in a function without an issue.

functions()
{
can contain more than
one line of code
to do what you need to do
with it. ;)
}

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 28 Nov 2014, 16:42

This is a snippet I'm trying.
But when I run it, I get the message "Error at line 16, Line text :Fdit
Error: This line does not contain a recognised action.

Fdit(SoundBeep, 1500, 200) ;this is my function
(Sleep, 80)

Loop,
{

Input,Key,L1

if(key=e)

Fdit
MsgBox, % " """ Key """ "
break

}

return

ExitApp

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 28 Nov 2014, 17:04

Try:

Code: Select all

Input,Key,L1
if (key="e")
{
    Fdit()
}
MsgBox, Done
ExitApp


Fdit()
{
    Sleep, 80
    SoundBeep, 1500, 200
}

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 28 Nov 2014, 17:17

More thanks, kon
I've tried your code and it works, and I've added in my MsgBox command to show the key pressed, which also works.
I'll have another session tomorrow.

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: trouble with script I am trying to develop for Morse Cod

Post by garry » 30 Nov 2014, 16:24

I was trying this , with many if's ... should use arrays as Kon recommended
only letters A-Z / a-z and digits 0-9

Code: Select all

;-------- saved at Montag, 1. Dezember 2014 20:17:07 --------------
;-------- http://ahkscript.org/boards/viewtopic.php?f=5&t=5259 ---
MODIFIED=20141201/4
;http://ahkscript.org/boards/viewtopic.php?f=5&t=5259

;-EDIT -mark character blue while sending and add EDIT for sended characters
;-EDIT defined dit / dah and soundlevel for PC Beep

/*
The relationship between Morse dots and dashes (Usually known as dits and dahs in Ham Radio) from the ITU Morse Code recommendations,
Dit = basic unit length
Dah = three Dits
Space between the elements forming the same letter = one Dit
Space between letters =three Dits
Space between words  = seven Dits
*/

dit:=130
dah:=(3*dit)
sbw:=(7*dit)
i=0
Gui,1:default
Gui,1: Font,CDefault,Fixedsys

;- sndvol32 set PC beep XP
soundset,0 ,Analog:1,mute  ;- 'PC Beep' ON
SoundSet,5 ,Analog:1       ;- set 'PC Beep' level low
;run,sndvol32              ;- check volume levels
StringCaseSense, Off
;Gui, Add, Edit  , x5   y10 w800 h50 vUpcx hwndhEdit hscroll -wrap +0x100,SOS A`nsos B`n
Gui, Add, Edit  , x5   y10  w800 h50 vUpcx hwndhEdit +0x100,Sos z`nA`nB`nEnd
Gui, Add, Button, x5   y70  w150 h20 gClear1,ClearScreen
Gui, Add, Button, x5   y100 w150 h20 gOk1   ,MORSE
Gui, Add, Button, x5   y130 w150 h20 gBreak1,Break
Gui, add, Text  , x200 y90 w95  vT1
Gui, add, Edit  , x300 y90 w50  vE1 right
Gui, add, Edit  , x360 y90 w50  vE2 right
GuiControl,1:Focus,upcx
Gui, Show, x50 y50 w820 h160,MORSE-Test
Return
Guiclose:
exitapp

clear1:
GuiControl,1:,upcx,
GuiControl,1:,e1
GuiControl,1:,e2
GuiControl,1:,t1
return

break1:
GuiControl,1:,t1,Break
GuiControl,1:,e1
GuiControl,1:,e2
return


Ok1:
GuiControlGet,S1,,upcx
stringupper,s1,s1         ;- convert lower to upper
StringSplit,a,S1
GuiControl,1:,T1,Sending ....
s2=
i=0
Loop,%a0%
  {
  GuiControlGet,S2,,T1
  if (s2="Break")
     break
  Sub:=a%A_Index%
  n:=asc(sub)      ;- 65--90 A-Z  48--57 0-9  ASCII
  ;msgbox, 262144, ,%n%,1
; 65  66    67    68   69 70    71   72    73  74    75   76    77  78  79   80    81    82   83   84 85   86    87   88    89    90
; A   B     C     D    E  F     G    H     I   J     K    L     M   N   O    P     Q     R    S    T  U    V     W    X     Y     Z
; .-, -..., -.-., -.., ., ..-., --., ...., .., .---, -.-, .-.., --, -., ---, .--., --.-, .-., ..., -, ..-, ...-, .--, -..-, -.--, --..

         GuiControl,1:,T1,Send

         if (n=32)       ;- space between words
            {
            x=32
            sub=SPACE
            gosub,aac
            GuiControl,1:,T1,ASCII
            sleep,%sbw%
            continue
            }

         if (n=10)       ;- LF
            {
            x= 10
            sub=LF
            i:=(i+2)
            GuiControl,1:,T1,ASCII
            GuiControl,1:,E1,%sub%
            GuiControl,1:,E2,%x%
            sleep,%sbw%
            continue
            }

         if (n=65)       ;-A= .-
            {
            x= .-
            gosub,aac
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=66)       ;-B= -...
            {
            x= -...
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=67)       ;-C= -.-.
            {
            x= -.-.
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=68)        ;-D= -..
            {
            x= -..
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=69)        ;-E= .
            {
            x= .
            gosub,aac
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=70)        ;-F= ..-.
            {
            x= ..-.
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=71)        ;-G= --.
            {
            x= --.
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=72)          ;-H= ....
            {
            x= ....
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=73)          ;-I= ..
            {
            x= ..
            gosub,aac
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=74)          ;-J= .---
            {
            x= .---
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=75)          ;-K= -.-
            {
            x= -.-
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=76)          ;-L= .-..
            {
            x= .-..
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=77)          ;-M= --
            {
            x= --
            gosub,aac
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=78)          ;-N= -.
            {
            x= -.
            gosub,aac
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=79)          ;-O= ---
            {
            x= ---
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=80)          ;-P= .--.
            {
            x= .--.
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=81)          ;-Q= --.-
            {
            x= --.-
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=82)          ;-R= .-.
            {
            x= .-.
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=83)          ;-S= ...
            {
            x= ...
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=84)          ;-T= -
            {
            x= -
            gosub,aac
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=85)          ;-U= ..-
            {
            x= ..-
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=86)          ;-V= ...-
            {
            x= ...-
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=87)          ;-W= .--
            {
            x= .--
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=88)           ;-X= -..-
            {
            x= -..-
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=89)           ;-Y= -.--
            {
            x= -.--
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=90)           ;-Z= --..
            {
            x= --..
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }

; 48    49    50    51    52    53    54    55    56    57
; 0     1     2     3     4     5     6     7     8     9
; -----,.----,..---,...--,....-,.....,-....,--...,---..,----.


         if (n=48)           ;-0= -----
            {
            x= -----
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=49)           ;-1= .----
            {
            x= .----
            gosub,aac
            gosub,ss
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=50)           ;-2= ..---
            {
            x= ..---
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,sl
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=51)           ;-3= ...--
            {
            x= ...--
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,sl
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=52)           ;-4= ....-
            {
            x= ....-
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,sl
            sleep,%dah%
            continue
            }
         if (n=53)           ;-5= .....
            {
            x= .....
            gosub,aac
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=54)           ;-6= -....
            {
            x= -....
            gosub,aac
            gosub,sl
            gosub,ss
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=55)           ;-7= --...
            {
            x= --...
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,ss
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=56)           ;-8= ---..
            {
            x= ---..
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,ss
            gosub,ss
            sleep,%dah%
            continue
            }
         if (n=57)           ;-9= ----.
            {
            x= ----.
            gosub,aac
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,sl
            gosub,ss
            sleep,%dah%
            continue
            }
  }
if (s2="break")
  GuiControl,1:,T1,Break
else
  GuiControl,1:,T1,Finished
Return
;-----------------------


/*
The relationship between Morse dots and dashes (Usually known as dits and dahs in Ham Radio) from the ITU Morse Code recommendations,
Dit = basic unit length
Dah = three Dits
Space between the elements forming the same letter = one Dit
Space between letters =three Dits
Space between words  = seven Dits

;- see above
dit:=130
dah:=(3*dit)
sbw:=(7*dit)
*/


ss:                     ;- dit
SoundBeep, 404, %dit%
sleep,%dit%
return

sL:                     ;- dah
SoundBeep, 400, %dah%
sleep,%dit%
return
;-----------------------------------------------------



;-- set character blue which is sending --------
aac:
GuiControl,1:,T1,Send
GuiControl,1:,E1,%sub%
GuiControl,1:,E2,%x%
p1:=(i)
p2:=(i+1)
;msgbox, 262208,TEST ,P1=%p1%`nP2=%p2%, 2
   SendMessage, 0xB1, p1, p2,, ahk_id %hEdit%   ;- 0xB1 is EM_SETSEL    ;<< mark character blue
   SendMessage  0xB7,  0,  0,, ahk_id %hEdit%   ;- EM_SCROLLCARET       ;<< added this line to scroll ( from user space )
sleep,10
i++
return
;=====================================================




/*
10 REM Morse code - MORSE.BAS  GWBASIC
20 CLEAR :CLS :KEY OFF
30 PRINT "Type a message and see it appear as Morse Code." :PRINT
40 DIM CODE$(122)
50 RESTORE
60 FOR A = 32 TO 122
70   READ CODE$(A)
80   PRINT CHR$(A);"  ";
90 NEXT A
100 PRINT :PRINT
110 INPUT "Use any of the above - What is your message "; MESSAGE$
120 PRINT
130 L = LEN(MESSAGE$)
140 FOR B = 1 TO L
150   PRINT CODE$(ASC(MID$(MESSAGE$,B,1)));" ";
160 NEXT B
170 PRINT :PRINT
180 END
190 DATA " ","!","|","#","$","%","&","'","(",")",".","+",",","-","   ","/"
200 DATA -----,.----,..---,...--,....-,.....,-....,--...,---..,----.
210 DATA ":",";","<","=",">","?","@"
220 DATA .-, -..., -.-., -.., ., ..-., --., ...., .., .---, -.-, .-.., --
230 DATA -., ---, .--., --.-, .-., ..., -, ..-, ...-, .--, -..-, -.--, --..
240 DATA "[","\","]","^","_","'"
250 DATA ._, -..., -.-., -.., ., ..-., --., ...., .., .---, -.-, .-.., --
260 DATA -., ---, .--., --.-, .-., ..., -, ..-, ...-, .--, -..-, -.--, --..

RUN
Type a message and see it appear as Morse Code.

   !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /  0  1  2  3  4  5  6  7  8  9  :
  ;  <  =  >  ?  @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T
U  V  W  X  Y  Z  [  \  ]  ^  _  `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
  p  q  r  s  t  u  v  w  x  y  z

Use any of the above - What is your message ? ABCD SOS

.- -... -.-. -..   ... --- ...

Ok
*/
;================================================
Last edited by garry on 01 Dec 2014, 14:53, edited 1 time in total.

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 01 Dec 2014, 07:14

That is a great script garry, thanks.
I have developed kon's Array example to recall the dits and dahs for the letters a-z, but am struggling with 0-9,(Not NumPad) as the MsgBox just shows blank.
I love AHK as it gives instant gratification/desperation, when trying stuff.
I have learned an awful lot just trying to find out why something has not worked, and scouring the Bulletin Boards for a solution.
Thanks again to all, for taking the time to help beginners.

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 01 Dec 2014, 14:00

spexnut wrote:I have developed kon's Array example to recall the dits and dahs for the letters a-z, but am struggling with 0-9,(Not NumPad) as the MsgBox just shows blank.
Like this?

Code: Select all

Var := "132"
Loop, Parse, Var
{
	MyFunction(A_LoopField)
}
return

MyFunction(x) {
	static MyArray := {1: ". - - - -", 2: ". . - - -", 3: ". . . - -"}
	String := MyArray[x]
	MsgBox, % String
}

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 06 Dec 2014, 13:32

This is how far I've got with the script (or do we call it code?) up 'til now.
It's probably very clunky, and I've still got some difficulties I could use some advice with.
1, No matter where I put Esc::ExitApp, (At the very end of the script, as recommended, it does nothing), and anywhere else it just messes

things up.
2, I haven't yet been able to get the key pressed to ignore any keys that are not in the array, as any of these produce an empty MsgBox.

The biggest problem however, is the ExitApp one.

This is my code so far.
By the way can someone show me step-by-step how to syntax highlight the code I post to these bulletin boards?

Code: Select all

 
; This is the Array containing the Morse Code equivalents of the keypresses.
MorseArray := {a: "dit dah", b: "dah dit dit dit", c: "dah dit dah dit", d: "dah dit dit", e: "dit", f: "dit dit dah dit", g: "dah dah dit", h: "dit dit dit dit", i: "dit dit", j: "dit dah dah dah", k: "dah dit dah", l: "dit dah dit dit", m: "dah dah",n: "dah dit",  o: "dah dah dah", p: "dit dah dah dit", q: "dah dah dit dah", r: "dit dah dit", s: "dit dit dit", t: "dah", u: "dit dit dah",v: "dit dit dit dah", w: "dit dah dah", x: "dah dit dit dah", y: "dah dit dah dah", z: "dah dah dit dit",0: "dah dah dah dah dah", 1: "dit dah dah dah dah", 2: "dit dit dah dah dah", 3: "dit dit dit dah dah", 4: "dit dit dit dit dah", 5: "dit dit dit dit dit", 6: "dah dit dit dit dit", 7: "dah dah dit dit dit", 8: "dah dah dah dit dit", 9: "dah dah dah dah dit"}


Loop,
{
Input, Key, L1	; Get the key which has been pressed.

;MsgBox, , , % Key, 1	; This works OK but I've temporarily disabled it while developing the rest of the code.

value := MorseArray[Key] ; Find the value for the key in the array MorseArray.


Loop, Parse, value, %a_space%	; Parse the value from the array to find dits and dahs.

	If (a_loopfield = "dit")	; If a dit is found, call Function Fdit.
			{
				Fdit()
			}
	;MsgBox, , , "It is a Dit", 4	; I used this during the code development.
else
	If (a_loopfield = "dah")	; If a dah is found, call function Fdah.
		
			{
				Fdah()
			}
	;MsgBox, , , "It is a Dah", 4	; I used this during the code development.
;else
	
	;MsgBox % a_loopfield	; I used this during the code development.

; Here are the Functions to convert dits & dahs into SoundBeeps of the right tone and length.

Fdit()	; Function for the sound of a dit.
	{    
    SoundBeep, 1500, 200
    Sleep, 80
	return
	}
	
Fdah()	; Function for the sound of a dah.
	{    
    SoundBeep, 1500, 600
    Sleep, 80
	return
	}
	
	
}


;Esc::ExitApp

; Still to do, display the key which has been pressed in a MsgBox, (Or a colour Gui with a TimeOut) (This works at the moment).
; I have managed to do this as a code snippet, and need to incorporate it in the full program, as well as a Gui with a TimeOut to
; explain the use of the program.I want to use a Gui, as this gives me control over the background colour and font size/colour
; Get Esc::ExitApp to work properly.
; Filter out keys which are not in the MorseArray.


kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 06 Dec 2014, 14:04

This is great! Well done! :clap:
spexnut wrote:1, No matter where I put Esc::ExitApp, (At the very end of the script, as recommended, it does nothing), and anywhere else it just messes

things up.
The Input command is interfering with the hotkey. Try using the $ prefix.
$Esc::ExitApp
This forces the use of the keyboard hook, and is equivalent to having used #UseHook
spexnut wrote:2, I haven't yet been able to get the key pressed to ignore any keys that are not in the array, as any of these produce an empty MsgBox.
After this line value := MorseArray[Key] try this:

Code: Select all

if (!value)
    continue
This will skip the rest of the current loop if "value" is blank or equal to zero.
spexnut wrote:By the way can someone show me step-by-step how to syntax highlight the code I post to these bulletin boards?
When using the code tags: ["code]this is code[/code"] will produce plain text, ["code=autohotkey file=Script.ahk]this is code[/code"]will use AutoHotkey syntax highlighting. (Remove the "")
Also, when posting, click the "Full Editor" button at the bottom of the page. This gives you more options, including the ability to use the "preview" button.

Hope it helps :)

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 06 Dec 2014, 15:41

Thanks kon, I've got a bit further, however when I run my code there are two problems.

1, The opening screen Gui shows as it should at the beginning, but also shows instead of the key pressed Gui.

2, If two keys are pressed in quick succession, the script hangs, or the extra key is written into the code, wherever the cursor is.

What have I done wrong?

Code: Select all


; This is the Array containing the Morse Code equivalents of the keypresses.
MorseArray := {a: "dit dah", b: "dah dit dit dit", c: "dah dit dah dit", d: "dah dit dit", e: "dit", f: "dit dit dah dit", g: "dah dah dit", h: "dit dit dit dit", i: "dit dit", j: "dit dah dah dah", k: "dah dit dah", l: "dit dah dit dit", m: "dah dah",n: "dah dit",  o: "dah dah dah", p: "dit dah dah dit", q: "dah dah dit dah", r: "dit dah dit", s: "dit dit dit", t: "dah", u: "dit dit dah",v: "dit dit dit dah", w: "dit dah dah", x: "dah dit dit dah", y: "dah dit dah dah", z: "dah dah dit dit",0: "dah dah dah dah dah", 1: "dit dah dah dah dah", 2: "dit dit dah dah dah", 3: "dit dit dit dah dah", 4: "dit dit dit dit dah", 5: "dit dit dit dit dit", 6: "dah dit dit dit dit", 7: "dah dah dit dit dit", 8: "dah dah dah dit dit", 9: "dah dah dah dah dit"}

Fopening_screen()

/*Gui, Color, Yellow	; Creates a Gui with a yellow background.
Gui, Font,,Verdana	; Sets the font to Verdana.
Gui, Font, s30		; Sets the font size to 30 point. (Quite large) 
Gui, Add, Text, cBlue, Press any key from  a to z`nor 0 to 9,	; Explains what the script does and how to exit it, sets the font colour to blue. 
and you will hear the`nMorse Code equivalent for that key.`nPress the ESC key to Exit	; As above.

SetTimer TimeOut, 4000	; Sets a time (4 seconds)for the Gui to remain visible before closing itself.

Gui, Show,		; The code which does the above.
Return
Gui, Destroy	; Destroys the Gui after the time set above has elapsed.

TimeOut:		; Tells the Gui to close as per the above.
Gui, Cancel
;return
;ExitApp
*/




;return

Loop {

Input ,Key, L1

Fkey_pressed()

/*Gui, Color, Yellow	; Creates a Gui with a yellow background.
Gui, Font,,Verdana	; Sets the font to Verdana.
Gui, Font, s30		; Sets the font size to 30 point. (Quite large)
Gui, Add, Text, cBlue, %Key%	; Sets the font colour to blue, and displays the key that has been pressed.

SetTimer TimeOut2, 3000	; Times out the Gui after 3 seconds.

Gui, Show , Center	; This is the code which shows the Gui in the Centre of the screen.
Return
Gui, Destroy		; Destroys the Gui after the time set above has elapsed.

TimeOut2:			; ; Tells the Gui to close as per the above.
Gui, Cancel
;ExitApp
*/



;return
;MsgBox, , , % Key, 1	; This works OK but I've temporarily disabled it while developing the rest of the code.

value := MorseArray[Key] ; Find the value for the key in the array MorseArray.

If (!value)	; This screens out any keypressed which is not in MorseArray
	
	continue



Loop, Parse, value, %a_space%	; Parse the value from the array to find dits and dahs.

	If (a_loopfield = "dit")	; If a dit is found, call Function Fdit.
			{
				Fdit()
			}
	;MsgBox, , , "It is a Dit", 4	; I used this during the code development.
else
	If (a_loopfield = "dah")	; If a dah is found, call function Fdah.
		
			{
				Fdah()
			}
	;MsgBox, , , "It is a Dah", 4	; I used this during the code development.
;else
	
	;MsgBox % a_loopfield	; I used this during the code development.

	; Here are the Functions to convert dits & dahs into SoundBeeps of the right tone and length.
	; and the Functions for the opening screen and key pressed.


Fopening_screen()
{
Gui, Color, Yellow	; Creates a Gui with a yellow background.
Gui, Font,,Verdana	; Sets the font to Verdana.
Gui, Font, s30		; Sets the font size to 30 point. (Quite large) 
Gui, Add, Text, cBlue, Press any key from  a to z`nor 0 to 9,	; Explains what the script does and how to exit it, sets the font colour to blue. 
and you will hear the`nMorse Code equivalent for that key.`nPress the ESC key to Exit	; As above.

SetTimer TimeOut, 4000	; Sets a time (4 seconds)for the Gui to remain visible before closing itself.

Gui, Show,		; The code which does the above.
Return
Gui, Destroy	; Destroys the Gui after the time set above has elapsed.

TimeOut:		; Tells the Gui to close as per the above.
Gui, Cancel
return
}
;return

Fkey_pressed()
{
Gui, Color, Yellow				; Creates a Gui with a yellow background.
Gui, Font,,Verdana				; Sets the font to Verdana.
Gui, Font, s30					; Sets the font size to 30 point. (Quite large)
Gui, Add, Text, cBlue, %Key%	; Sets the font colour to blue, and displays the key that has been pressed.

SetTimer TimeOut2, 3000	; Times out the Gui after 3 seconds.

Gui, Show , Center	; This is the code which shows the Gui in the Centre of the screen.
Return
Gui, Destroy		; Destroys the Gui after the time set above has elapsed.

TimeOut2:			; ; Tells the Gui to close as per the above.
Gui, Cancel
return
}



Fdit()	; Function for the sound of a dit.
	{    
    SoundBeep, 1500, 200
    Sleep, 80
	return
	}
	
Fdah()	; Function for the sound of a dah.
	{    
    SoundBeep, 1500, 600
    Sleep, 80
	return
	}
	
return	
}


$Esc::ExitApp	; The addition of the $ before the Esc forces the use of the keyboard hook, and is equivalent to having used #UseHook.
				; Thanks kon.

; Still to do, display the key which has been pressed in a MsgBox, (Or a colour Gui with a TimeOut) (This works at the moment).
; I have managed to do this as a code snippet, and need to incorporate it in the full program, as well as a Gui with a TimeOut to
; explain the use of the program.I want to use a Gui, as this gives me control over the background colour and font size/colour
; Get Esc::ExitApp to work properly.
; Filter out keys which are not in the MorseArray. Done Thanks kon.
; If the keys are pressed in quick succession, the script hangs/falters

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 06 Dec 2014, 17:07

The loop has a return on the line prior to its closing brace. The return causes the script to stop at that point.

In the "Timeout" subroutine, move Gui, Destroy from its current position to below the label. At its current position it will not be executed.
2, If two keys are pressed in quick succession, the script hangs, or the extra key is written into the code, wherever the cursor is.
When the GUI is being displayed, the script is already waiting at the "Input" line for the next key to be pressed. Also, when the script is playing the beeps, the Input command is not active, so keypresses are not caught by the script and instead simply go where they normally would. You could use a complicated combination of Blockinput commands, but I favor the following approach:
I am going to suggest abandoning your outermost Loop and replacing Input. Instead make every key (a-z and 0-0) a hotkey.

So after your first gui is done you would do something like this:

Code: Select all

global Running	;make this variable global so it is still visible inside the functions
Loop, 26	; Make the keys A-Z into hotkeys
	Hotkey, % Chr(A_Index + 64), Start

Loop, 10 ; Make the keys 0-9 into hotkeys
	Hotkey, % A_Index - 1, Start
return


Start:          ;replace the outer loop and Input command with this label
if (Running)	; if a hotkey is already running, do nothing
	return
Running := true
Fkey_pressed(A_ThisHotkey)
value := MorseArray[A_ThisHotkey] ; Find the value for the key in the array MorseArray.
;...play sounds ...etc
return   ; end of hotkey
And in your Gui function, you would do:

Code: Select all

Fkey_pressed(Key)
{
	Gui, Color, Yellow              ; Creates a Gui with a yellow background.
	Gui, Font,,Verdana              ; Sets the font to Verdana.
	Gui, Font, s30                  ; Sets the font size to 30 point. (Quite large)
	Gui, Add, Text, cBlue, %Key%    ; Sets the font colour to blue, and displays the key that has been pressed.

	SetTimer TimeOut2, -3000 		; Times out the Gui after 3 seconds.

	Gui, Show , Center, Display Letter  ; This is the code which shows the Gui in the Centre of the screen.
	Return


	TimeOut2:           ; Tells the Gui to close as per the above.
	Gui, Destroy
	Running := false	; Allows another hotkey to be pressed now
	return
}
Notice how I changed the value in SetTimer to a negative number? This is so the timer only runs once. Otherwise it will run continuously and close your Gui at seemingly random times. (change the one in your other function too)




I see that Fdit and Fdah do almost the same thing. The only difference is the length of the beep. You could combine them into one function like this:

Code: Select all

PlaySound(Length)
{
	SoundBeep, 1500, %Length%
	Sleep, 80
	return
}
And call the function like this:

Code: Select all

		If (a_loopfield = "dit")
		{
			PlaySound(200)
		}
		else If (a_loopfield = "dah")
		{
			PlaySound(600)
		}

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 07 Dec 2014, 14:06

Hi kon, In the old adage of only trying one thing at a time I am working through your suggestions.
I have tried moving the Gui, Destroy as you suggested, but now the correct Gui shows, but it is blank.
Could you please clarify.
Sorry about all the commented rubbish I uploaded as well as the code with my last post, It was way past my bedtime. ;)
Thanks.

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: trouble with script I am trying to develop for Morse Cod

Post by kon » 07 Dec 2014, 14:35

You need to pass "key" to the function.
So when calling the function it should look like: Fkey_pressed(Key)
You need to change the function definition so it accepts "key" too. See the code from my previous post, right after I said, "And in your Gui function, you would do."

spexnut
Posts: 11
Joined: 17 Nov 2014, 14:16
Location: England U.K.

Re: trouble with script I am trying to develop for Morse Cod

Post by spexnut » 07 Dec 2014, 16:27

Thanks, I must still be doing something wrong as I was only getting dits and no dahs, now after some fiddling I get neither.
And one question, where does the keypressed get converted to capital letters, not lower case?
It doesn't affect the use of the program in any way, I am just intrigued.

Code: Select all


; This is the Array containing the Morse Code equivalents of the keypresses.
MorseArray := {a: "dit dah", b: "dah dit dit dit", c: "dah dit dah dit", d: "dah dit dit", e: "dit", f: "dit dit dah dit", g: "dah dah dit", h: "dit dit dit dit", i: "dit dit", j: "dit dah dah dah", k: "dah dit dah", l: "dit dah dit dit", m: "dah dah",n: "dah dit",  o: "dah dah dah", p: "dit dah dah dit", q: "dah dah dit dah", r: "dit dah dit", s: "dit dit dit", t: "dah", u: "dit dit dah",v: "dit dit dit dah", w: "dit dah dah", x: "dah dit dit dah", y: "dah dit dah dah", z: "dah dah dit dit",0: "dah dah dah dah dah", 1: "dit dah dah dah dah", 2: "dit dit dah dah dah", 3: "dit dit dit dah dah", 4: "dit dit dit dit dah", 5: "dit dit dit dit dit", 6: "dah dit dit dit dit", 7: "dah dah dit dit dit", 8: "dah dah dah dit dit", 9: "dah dah dah dah dit"}

Fopening_screen()

global Running  	;make this variable global so it is still visible inside the functions
Loop, 26    		; Make the keys A-Z into hotkeys
    Hotkey, % Chr(A_Index + 64), Start

Loop, 10 			; Make the keys 0-9 into hotkeys
    Hotkey, % A_Index - 1, Start
return


Start:         		;replace the outer loop and Input command with this label
if (Running)    	; if a hotkey is already running, do nothing
    return
Running := true
Fkey_pressed(A_ThisHotkey)
value := MorseArray[A_ThisHotkey] ; Find the value for the key in the array MorseArray.


					;...play sounds ...etc
return   			; end of hotkey



Loop, Parse, value, %a_space%		; Parse the value from the array to find dits and dahs.

	If (a_loopfield = "dit")		; If a dit is found, call function PlaySound(Length).
		{			
			PlaySound(Length)
		}	
			
	;else
	
	If (a_loopfield = "dah")		; If a dah is found, call function PlaySound(Length).
		{	
			PlaySound(Length)
		}	
	
	;return
	;MsgBox % a_loopfield			; I used this during the code development.

	


Fopening_screen()
	{
		
Gui, Color, Yellow				; Creates a Gui with a yellow background.
Gui, Font,,Verdana				; Sets the font to Verdana.
Gui, Font, s30					; Sets the font size to 30 point. (Quite large) 
Gui, Add, Text, cBlue, Press any key from  a to z`nor 0 to 9,	; Explains what the script does and how to exit it, sets the font colour to blue. 
and you will hear the`nMorse Code equivalent for that key.`nPress the ESC key to Exit	; As above.

SetTimer TimeOut1, -3000		; Sets a time (3 seconds)for the Gui to remain visible before closing itself.
								; Negative value allows TimeOut to execute once only.
Gui, Show,						; The code which does the above.
return
								

TimeOut1:
Gui, Destroy					; Destroys the Gui after the time set above has elapsed.							
								
Gui, Cancel						; Tells the Gui to close as per the above.

return
	}


Fkey_pressed(Key)
{
    Gui, Color, Yellow              ; Creates a Gui with a yellow background.
    Gui, Font,,Verdana              ; Sets the font to Verdana.
    Gui, Font, s30                  ; Sets the font size to 30 point. (Quite large)
    Gui, Add, Text, cBlue, %Key%    ; Sets the font colour to blue, and displays the key that has been pressed.

    SetTimer TimeOut2, -3000        ; Times out the Gui after 3 seconds.
			; Negative value allows TimeOut to execute once only.	
    Gui, Show , Center, Display Letter  ; This is the code which shows the Gui in the Centre of the screen.
    Return


    TimeOut2:          	; Tells the Gui to close as per the above.
    Gui, Destroy
    Running := false    ; Allows another hotkey to be pressed now
    return
}


Loop,
        If (a_loopfield = "dit")
        {
            PlaySound(200)
        }
        else If (a_loopfield = "dah")
        {
            PlaySound(600)
        }
	
return

	PlaySound(Length)
{
    SoundBeep, 1500, %Length%
    Sleep, 80
    return
}	

$Esc::ExitApp	; The addition of the $ before the Esc forces the use of the keyboard hook, and is equivalent to having used #UseHook.
				


geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: trouble with script I am trying to develop for Morse Cod

Post by geek » 07 Dec 2014, 16:43

Here's the code I'm using for my Morse Code Christmas Tree, which follows Wikipedia's diagram of International Morse Code:

Image

Code: Select all

http := ComObjCreate("WinHttp.WinHttpRequest.5.1")

Input =
(
a.-
b-...
c-.-.
d-..
e.
f..-.
g--.
h....
i..
j.---
k-.-
l.-..
m--
n-.
o---
p.--.
q--.-
r.-.
s...
t-
u..-
v...-
w.--
x-..-
y-.--
z--..
)

Morse := []
for each, line in StrSplit(Input, "`n", "`r")
	Morse[SubStr(Line, 1, 1)] := SubStr(Line, 2)

Unit := 500
for each, Char in StrSplit("merry christmas")
{
	if !Morse.HasKey(Char)
	{
		Sleep, % Unit * 4 ; 3+4==7 units between words
		continue
	}
	ToolTip, % Char
	for each, Blip in StrSplit(Morse[Char])
	{
;		SoundBeep,, Unit * (Blip = "." ? 1 : 3)
		TreeOn()
		Sleep, % Unit * (Blip = "." ? 1 : 3) ; dot or dash
		TreeOff()
		Sleep, % Unit * 1 ; 1 unit between dots and dashes
	}
	Sleep, % Unit * 2 ; 1+2==3 units between words
}

TreeOn()
{
	global http
	http.Open("GET", "http://ev3/?on"),http.Send()
}

TreeOff()
{
	global http
	http.Open("GET", "http://ev3/?off"),http.Send()
}
Doesn't include numbers, but it'd be trivial to add them

Post Reply

Return to “Ask for Help (v1)”