June 13, 2024, 12:42:50 AM


Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

Detecting the Enter key in an Edit Control

Started by GWS, February 10, 2015, 10:37:23 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.



I thought this little bit of magic was worth reviewing .. :)

If you've ever tried it, you will know that pressing the Enter key, while an Edit Control has the focus, will not process whatever you have typed into it. ::)  Microsoft never saw fit to generate a message for this common user action - all you get is an 'error' beep.

Unbelievable :o.  Note IWB has a specially included statement SETCONTROLNOTIFY to provide a way to detect the Enter key.  Unfortunately, this is not available in Creative.

So the easiest way round this, is to have a dedicated button in your program to process whatever you've typed into the Edit control. It's foolproof, but a bit clunky. ;D

I'm working on a program where the usual way of just pressing Enter should process the data without any fuss and extra buttons. :P

So that's where this magic method comes in.  Goodness knows how it works, but it does.

It seems that although Windows does not generate a message from an Edit control when the Enter key is pressed, it does send a message via the Menu system - whether you have a menu in your Window or not.

This message can be intercepted using the @IDMenuPick message, and acting on an if (@code = @IDOK) statement.

As a bonus, you can also use if (@code = @IDCancel) then run = 0 to exit the program using the ESCape key.

The following test program show how it works .. note that one other statement enabletabs w,1 is required once you've initialised the window w.

' Press Enter in an Edit Control - GWS 2015

def w:window
def wstyle,i:int

wstyle = @minbox

' open a window ..
window w,-600,0,600,400,wstyle,0,"Creative Basic",messages
setwindowcolor w,rgb(0,60,190)
centerwindow w

enabletabs w,1 :' ************ Needed to capture the Enter keypress

control w,"B,Exit,(600-70)/2,310,70,30,0,1"
control w,"E,,(600-100)/2,120,100,25,@cteditcenter,2"
control w,"T,,(600-100)/2,180,100,24,@cteditcenter|0x200,3"
setcontrolcolor w,3,0,0x777755
setfocus w,2

run = 1


SUB messages
select @class

' this section captures the Enter Key
' ************************************
case @IDMenuPick
' has the Enter key been pressed ..
if (@code = @IDOK)
' process the Edit control data ..
a$ = getcontroltext(w,2)
setcontroltext w,3,a$
frontpen w,0x00ffff
move w,150,185
print w,"You entered :"
setfocus w
' ************************************

' check for ESC key to exit ..
if (@code = @IDCancel) then run = 0

case @idclosewindow
run = 0
case @idcontrol
select @controlID
case 1
run = 0
case 2
if (@notifycode = @ENSETFOCUS) then setcontroltext w,2,""


It seems to work fine in XP - whether it works in later versions of Windows I do not know. I'd be interested if anyone tries it out.

Best wishes, :)

Tomorrow may be too late ..


Thanks for sharing Graham!

Tried your code on Win7Pro. Worked like a dream.

I had the same problem with ENTER in a RichEdit Control some time ago. Several methods were tried, but not all of them worked equally well, so finally ended up copying a method from the old IB Std days.
Don't remember if this ever was posted on this forum, so here it is:

Support Amateur Radio  -  Have a ham  for dinner!


My grandson just arrived, and we tried your code on his laptop running Win 8.1, loading the free version of CB from a USB memory stick.

No problems!
Support Amateur Radio  -  Have a ham  for dinner!


Hi Egil,

That's good to know - it seems to be solid across all the Window's versions then.

It's a bit weird, but only uses a couple of standard CBasic instructions, so it's pretty good.
Some other methods I've tried needed API routines and timers - a bit of overkill really.

I nearly bought an old Trio TS520, sold as 'not working' on Ebay - just for a project - but it went up to over £150 in auction.  I thought that was excessive for a load of electronic scrap, so I didn't bid in the end ..  ::)

All the best, :)

Tomorrow may be too late ..


Hi Graham!

A "not working" TS-520 is not worth many pounds...
It was a nice rig when it first were introduced back in the stone age, but today you get newer, and working, rigs for much less than £150.
Ebay auctions sometimes work in strange ways. I have seen used electronics sold there for much higher prices than what the equipment was sold for when it was brand new.
Must be something happening to peoples brains when they are bidding. I just don't understand it.

Support Amateur Radio  -  Have a ham  for dinner!


Thanks for your Short Dipole example Egil  ;D

You're very busy creating these interesting and useful amateur radio examples.

The 'short dipole' just about halves the length, from over 20m to 10m. Probably with the tee joint at the co-ax fairly high (maybe a V- formation), you would get about 5 metres of wire running at high current in the centre, and a bit off the ends.  Should be quite a decent signal. 

Don't know if you've tried one - just wondering about bandwidth ..  :) I would guess about 100KHz at that frequency :)

Couldn't see a RE (rich edit) control in the program though - maybe it was another program you were thinking of ..

Best wishes, :)

Tomorrow may be too late ..


February 13, 2015, 05:33:18 AM #6 Last Edit: February 13, 2015, 05:39:45 AM by Egil
It was tested by myself on 40 and 20. In an inverted-V configuration with the  top 12 meters up, SWR was below 2:1 over a bandwidth of 120 kHz on 40m, and around 200 kHz on 20 meters.
Others have tested it, and reported 40 kHz on 80m. With a good antenna tuner it should be possible to press the antenna into good service over the entire bands.

The idea for this calculator was found in a small book by John Heys (G3BDQ) called "Practical Wire Antennas".
He states that the dipoles can be cut to half size, if an inductor with  appproximately 950 ohms reactance is inserted exactly in the middle of each leg.
When I built one for 80 meters using  the dimensions given in his book, it worked so good that I decided to  make this calculator.
The inductance given in the drawing, is the value corresponding to 950 ohms. But to simplify calculations, I found that quarter turn resolution for the coil windings was more than good enough for practical work.

So, if you inspect my code, you'll find that the code add a quarter turn to the coil and calculates the reactance, and repeat this until the reactance passes 950 ohms.

Comparisons showed that the half size loaded dipole produced a signal only 4dB lower than a full size inverted-V dipole fed at the same height. This is less than 1 "S"!  And during normal band conditions this is barely noticeable.

I just love projects like  this!

QuoteCouldn't see a RE (rich edit) control in the program though - maybe it was another program you were thinking of ..

The very last part of the program is setting up the two RE-controls. But I did not like the appearance of them, so I gave them a "fake Edit Control look".... 8) 8)

Support Amateur Radio  -  Have a ham  for dinner!