how do you read a return key?
BlitzMax Forums/BlitzMax Beginners Area/how do you read a return key?
| ||
What I want to do here is get the window to close once the return key is pressed (so input some text and press return). But I'm not sure how to do it without adding a button to do so. Any help would be appreciated. At present I can only exit by pressing the close window gadget.Import maxgui.drivers Function save_as() ' Save current map under another name (duplication). Local window:TGadget=CreateWindow("New map name: (X aborts):",200,200,225,100,Null,WINDOW_TITLEBAR|WINDOW_CENTER) Local Label=CreateLabel("(3-15 chars.):",10,10,150,20,window) ' Name. Local name:tgadget=CreateTextField:tgadget(10,30,180,20,window) ActivateGadget(name) Repeat FlushKeys() ; FlushMouse() ; WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE Exit Case EVENT_GADGETACTION Select EventSource() Case name 'a$=GetChar() 'Print a$ ' If KeyDown(KEY_ENTER) Then Exit Local m$=Left$(TextFieldText(name),15) ' 15 chars max. m$=Replace$(m$,".","") ' Remove any full stops. ' If(Len(m$)<3) ' Must be at least 3. ' ActivateGadget(name) ' EndIf Print m$ End Select EndSelect Forever FreeGadget(window) End Function save_as() |
| ||
Hi, probably easiest is to use hidden ok button: Strict Import MaxGui.Drivers Local window:TGadget = CreateWindow( "ENTER KEY", 100, 100, 320, 240 ) Local textBox:tgadget = CreateTextField(10, 30, 80, 25, window) Local okButton:tgadget = CreateButton("", 0, 0, 0, 0, window, BUTTON_OK) Repeat WaitEvent() Select EventID() Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE End Case EVENT_GADGETACTION Select EventSource() Case textBox DebugLog "textBox" Case okButton Notify "Enter pressed!" EndSelect End Select Forever For more control you can use SetGadgetFilter() -Henri |
| ||
edit: damn, too late :)= Hi I'm quite sure (but maybe the problem was corrected) that filtering RETURN/ENTER key on a textfield is not possible. You should use a Button (BUTTON_OK) that 'intercepts' this key and follow this way. Import maxgui.drivers Global window:TGadget=CreateWindow("New map name: (X aborts):",200,200,225,100,Null,WINDOW_TITLEBAR|WINDOW_CENTER) Global Label=CreateLabel("(3-15 chars.):",10,10,150,20,window) ' Name. Global name:tgadget=CreateTextField(10,30,180,20,window) Global btn_ok:Tgadget= CreateButton("OK",10,100,20,20,window,BUTTON_OK) HideGadget btn_ok 'you could hide it, or just use 'over window' coordinates ActivateGadget btn_ok Repeat WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE Exit Case EVENT_GADGETACTION Select EventSource() Case btn_ok Print "Textfield content <"+GadgetText(name)+">" End Select End Select Forever End |
| ||
Thanks for both of your prompt answers. I really didn't want to have to do it with a button though. Perhaps some API programming is required to catch/filter the return key so. Not sure if there's a good guide here on that. Putting an exit after the print statement kind of works though, so it's a viable alternative. |
| ||
That ok cancel buttons did not work for me on linux so it isn't a solution I would trust "cross-platform-wise". Keep that in mind. Maybe you could define your key as a shortcut for an menu entry which closes the app? Bye Ron |
| ||
Yes, that's partly why i didn't want to use a button! If I find a solution I'll post it here for others to use. |
| ||
Hi, here is the SetGadgetFilter version. I realised that you need to replace textfield with textarea to catch enter key: Strict Import MaxGui.Drivers Local window:TGadget = CreateWindow( "ENTER KEY", 100, 100, 320, 240 ) Local textBox:tgadget = CreateTextArea(10, 30, 80, 25, window) SetGadgetFilter( textBox, filter ) Repeat WaitEvent() Select EventID() Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE End Case EVENT_GADGETACTION 'Do something End Select Forever Function filter:Int(event:TEvent,context:Object) If Not event Then Return False Select event.id Case EVENT_KEYDOWN If event.data = KEY_ENTER Then Notify("Enter pressed!") Return False EndIf Case EVENT_KEYCHAR 'Filter chars End Select Return True End Function -Henri |
| ||
Great, thanks so much Henri. We got there in the end! |
| ||
Just a slight change to Henri's code. This allows you to either exit from the textarea via the window close gadget or when a legal string has been input (note the flag variable):Import MaxGui.Drivers Global window:TGadget=CreateWindow("New map name: (X aborts):",200,200,225,100,Null,WINDOW_TITLEBAR|WINDOW_CENTER|WINDOW_STATUS) Global textBox:TGadget=CreateTextArea(10,30,180,20,window) Global flag=False Function save_as() Local Label=CreateLabel("(3-15 chars.):",10,10,150,20,window) ' Name. SetGadgetFilter(textBox,filter) ActivateGadget textBox Repeat WaitEvent() Select EventID() Case EVENT_WINDOWCLOSE Exit Default If(flag) Then Exit End Select Forever End Function Function filter(event:TEvent,context:Object) If(Not event) Then Return False Select event.id Case EVENT_KEYDOWN If(event.data=KEY_ENTER) ' Analyse text entry on a ENTER key. Local m$=Left$(GadgetText$(textBox),15) ' Filter 15 chars max. m$=Replace$(m$,".","") ' Filter any full stops. If(Len(m$)<3) ' Filter Must be at least 3 chars. SetStatusText window,"<3 legal characters. Try again." ; Delay 1000 ; SetStatusText window,"" Else ' Legal string requirements met. Print m$ ; FreeGadget(window) ; flag=True EndIf Return False EndIf End Select Return True End Function save_as() Thanks again everyone for your input. If you are wondering why I don't use strict or even superstrict, they lengthen the lines and thus I loose formatting sometimes when printing (so I prefer to keep lines of code short or within boundaries). |