About Monkey 2 › Forums › Monkey 2 Programming Help › Little mojox example – calculator
Tagged: calculator, mojox
This topic contains 8 replies, has 3 voices, and was last updated by 
 Simon Armstrong 2 years, 8 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 20, 2016 at 6:05 pm #3254
Ok, I’m trying to learning MX2 and mojox… so I start to create a simple calculator.
BUT, I’m locked.
I haven’t found any ‘gadget event’ handling (like in BlitzMax/MaxGUI), I know I can use ‘actions’ or some function pointer (or what is called), and now I’m stopped :/
ps: some thing in mojox must be ‘fixed’ I think – like the text alignment in textfield
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125#import "<std>"#import "<mojo>"#import "<mojox>"Using std..Using mojo..Using mojox..Global map_keys:=New StringMap<Button>Class MyWindow Extends WindowField txt_result:LabelFunction CreateMyButton:Button(_key:String,_title:String,view:DockingView)Local bn:=New Button(_title)map_keys.Set(_key,bn)'bn.Layout="float"bn.TextGravity=New Vec2f(.5,.5)'use Layout 'float'bn.Offset=New Vec2i(2,2)view.AddView(bn,"left",60)' bn.Clicked=GiveMeInfo() 'ops!Return bnEndMethod New()Super.New( "Calculator",320,200,WindowFlags.Center )#remresultx % 1 /7 8 9 *4 5 6 -1 2 3 ++ 0 , =#endLocal mainView:=New DockingViewLocal v_display:=New DockingViewLocal v_command:=New DockingViewLocal v_num3:=New DockingViewLocal v_num2:=New DockingViewLocal v_num1:=New DockingViewLocal v_num0:=New DockingViewmainView.MaxSize=New Vec2i(320,480 )CreateMyButton("+-","+-",v_num0)CreateMyButton("0","0",v_num0)CreateMyButton(".",".",v_num0)CreateMyButton("=","=",v_num0)CreateMyButton("1","1",v_num1)CreateMyButton("2","2",v_num1)CreateMyButton("3","3",v_num1)CreateMyButton("+","+",v_num1)CreateMyButton("4","4",v_num2)CreateMyButton("5","5",v_num2)CreateMyButton("6","6",v_num2)CreateMyButton("-","-",v_num2)CreateMyButton("7","7",v_num3)CreateMyButton("8","8",v_num3)CreateMyButton("9","9",v_num3)CreateMyButton("*","*",v_num3)CreateMyButton("CE","CE",v_command)CreateMyButton("C","C",v_command)CreateMyButton("<-","<-",v_command)CreateMyButton("/","/",v_command)txt_result=New Labeltxt_result.Text="0"txt_result.Layout="float"txt_result.TextGravity=New Vec2f(.99,.5) '<---- no working :/ ???txt_result.Style.SkinColor=Color.Greyv_display.AddView(txt_result,"left")mainView.AddView(v_display,"top")mainView.AddView(v_command,"top")mainView.AddView(v_num3,"top")mainView.AddView(v_num2,"top")mainView.AddView(v_num1,"top")mainView.AddView(v_num0,"top")' mainView.Layout="fill-x"ContentView=mainViewEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndThe idea was to check what button is pressed.
if it’s a numeric one (0,1,…9) just add to the result, if is another kind (+,- etc) doing the operation/deleting a char/etc…
Any help is appreciated
August 20, 2016 at 7:44 pm #3257There are some examples in mojox/bananas, edit hmm folder is actually named tests.
testbutton.monkey2:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#import "<std>"#import "<mojo>"#import "<mojox>"Using std..Using mojo..Using mojox..Class MyWindow Extends WindowMethod New()Super.New( "Button Demo",640,480,WindowFlags.Resizable )Local label:=New Label( "Idle" )label.Gravity=New Vec2f( .5,0 )Local button:=New Button( "Click ME!" )button.Clicked=Lambda()Global n:=0n+=1label.Text="Clicked #"+nEndbutton.DoubleClicked=Lambda()Global n:=0n+=1label.Text="Double clicked #"+nEndbutton.RightClicked=Lambda()Global n:=0n+=1label.Text="Right clicked #"+nEndLocal dockingView:=New DockingViewdockingView.AddView( label,"top" )dockingView.ContentView=buttonContentView=dockingViewEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndAugust 20, 2016 at 8:24 pm #3259interesting! I need to learn how to use Lambda.
I was trying to get degac’s code to work but run into problems. How do you access keys like the Plus key? I can access the equal key but not the Plus key which is a shift of the same key.
August 20, 2016 at 8:25 pm #3260Oh! Thanks!
I didn’t noticed that folder!
August 20, 2016 at 8:57 pm #3261Found a possible solution using Lambda() and a Global ref to txt_result! (this was the problem!)
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166#import "<std>"#import "<mojo>"#import "<mojox>"Using std..Using mojo..Using mojox..Global map_keys:=New StringMap<Button>Class MyWindow Extends Windowglobal txt_result:LabelFunction CreateMyButton:Button(_key:String,_title:String,view:DockingView)Local bn:=New Button(_title)map_keys.Set(_key,bn)'bn.Layout="float"bn.TextGravity=New Vec2f(.5,.5)'use Layout 'float'bn.Offset=New Vec2i(2,2)view.AddView(bn,"left",60)bn.Clicked=Lambda()If bnLocal vi:=Int(bn.Text)Local addv:=txt_result.TextLocal skip:IntSelect bn.TextCase "CE"txt_result.Text="0"skip=1Case "<-"addv=txt_result.Texttxt_result.Text=addv.Left(addv.Length-1)If txt_result.Text="" txt_result.Text="0"skip=1End selectIf skip=0If vi>=0 And vi<=9If addv="0" addv=""addv=addv+bn.Texttxt_result.Text=addvEnd IfEnd IfEnd IfEndReturn bnEndMethod New()Super.New( "Calculator",320,200,WindowFlags.Center )#remresultx % 1 /7 8 9 *4 5 6 -1 2 3 ++ 0 , =#endLocal mainView:=New DockingViewLocal v_display:=New DockingViewLocal v_command:=New DockingViewLocal v_num3:=New DockingViewLocal v_num2:=New DockingViewLocal v_num1:=New DockingViewLocal v_num0:=New DockingViewmainView.MaxSize=New Vec2i(320,480 )CreateMyButton("+-","+-",v_num0)CreateMyButton("0","0",v_num0)CreateMyButton(".",".",v_num0)CreateMyButton("=","=",v_num0)CreateMyButton("1","1",v_num1)CreateMyButton("2","2",v_num1)CreateMyButton("3","3",v_num1)CreateMyButton("+","+",v_num1)CreateMyButton("4","4",v_num2)CreateMyButton("5","5",v_num2)CreateMyButton("6","6",v_num2)CreateMyButton("-","-",v_num2)CreateMyButton("7","7",v_num3)CreateMyButton("8","8",v_num3)CreateMyButton("9","9",v_num3)CreateMyButton("*","*",v_num3)CreateMyButton("CE","CE",v_command)CreateMyButton("C","C",v_command)CreateMyButton("<-","<-",v_command)CreateMyButton("/","/",v_command)txt_result=New Labeltxt_result.Text="0"txt_result.Layout="float"txt_result.TextGravity=New Vec2f(.99,.5) '<---- no working :/txt_result.Style.SkinColor=Color.Greyv_display.AddView(txt_result,"left")mainView.AddView(v_display,"top")mainView.AddView(v_command,"top")mainView.AddView(v_num3,"top")mainView.AddView(v_num2,"top")mainView.AddView(v_num1,"top")mainView.AddView(v_num0,"top")' mainView.Layout="fill-x"ContentView=mainViewEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndOk, it’s not very ‘beautiful’ code, just written it to test if the logic works.
Now it’s time to sleep, thanks again!
August 20, 2016 at 9:01 pm #3262@Jessi: MMM… I don’t understand your question!
August 20, 2016 at 9:31 pm #3264I was trying to use the keyboard to control the input for the operations. I used OnKeyEvent and event.Key to access the the keyboard input but Event.Key does not return all the keys pressed. At least not in a way I can comprehend. It seems to me that it returns the keyboard button but not the key. for example on the qwerty keyboard the plus and equals sign are on the same key. so If I want to use the equal sign it’s fine. but if I want to use the plus sign it registers as the equal sign. I am probably missing something but don’t know.
August 21, 2016 at 9:26 am #3323Ok, I understood.
I still have no explored deep the ‘key’ event, but I’m quite sure the ‘key’ is just the button pressed on the keyboard…
Looking at the code & manual there’s a field TEXT in the keyevent returned that should have the ‘ascii character’ pressed I suppose!
Monkey1234Method OnKeyEvent( event:KeyEvent ) OverridePrint "Key Text : "+event.TextEndI just tested and if I press the ‘1’ key (on the mainboard) I got (many… depends on my pressure) ‘1’.
If I try to press SHIFT+1 (to print !) I got both SHIF LEFT and 1 …
I looked and mojo/input has a class KeyboardDevice… it should what needed.
I have to investigate
ps: it’s not an ‘error’ of MX2, it’s due to the different keyboard layout (and notebook are different again!)
August 21, 2016 at 9:52 am #3324I think you need to be testing for event type of EventType.KeyChar before using contents of event text.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.