About Monkey 2 › Forums › Monkey 2 Development › ScanCodeToRawKey
This topic contains 7 replies, has 4 voices, and was last updated by 
 bigand 1 year, 5 months ago.
- 
		AuthorPosts
 - 
		
			
				
November 6, 2017 at 12:02 pm #11519
I have just updated to V1.1.08 and the ScanCodeToRawKey now throws this error:
“Error : Internal declaration ‘ScanCodeToRawKey’ cannot be accessed from here.”
The code still works fine in V1.1.07. This is on Mac OS Sierra 10.12.6.
A quick example:
Function AnyKey:Key()
Local i:Int,k:KeyFor i=0 To 255
k=Keyboard.ScanCodeToRawKey(i)
If Keyboard.KeyDown(k)
Return(k)
Endif
Next
Return(Key.None)
EndNovember 7, 2017 at 1:41 am #11527ScanCodeToRawKey is an internal function – it has been @hidden from docs, and should therefore be considered ‘use art your own risk’.
I’ve had to make it public in the past as monkey2 lacked an ‘Internal’ directive, ie: decls are visible to other module members only. But that has recently been added to monkey so I have made several methods/functions Internal.
What are you actually using it for?
November 7, 2017 at 8:15 am #11528Thanks for the info Mark.
Just using like the function above. Couldn’t think of a better way to check for “Any Key” being pressed.
Its not critical at all, it was just useful.
November 7, 2017 at 9:40 am #11529You can specify own application key filter and grab any keys:
Monkey1234App.KeyEventFilter += Lambda( event:KeyEvent )' do something by key eventEndNovember 7, 2017 at 10:44 am #11530@nerobot erm. ok. why?
Why not use:
Method OnKeyEvent( event:KeyEvent ) Override
end methodWhich is the normal way of trapping key codes. But not in this case cause he wants to check if a key is pressed, hence using scancode
November 7, 2017 at 10:46 am #11531You can use App.KeyEventFilter in any place, but OnKeyEvent only inside of View-based classes like Window.
What if my key-logic know nothing about window?
November 7, 2017 at 11:08 pm #11535Just using like the function above. Couldn’t think of a better way to check for “Any Key” being pressed.
You don’t need to pass rawkeys to KeyDown, you can just pass a plain ‘virtual’ key, eg: ‘Key.A’ or ‘Key.Z’ etc.
But to pass an int ‘key’ to KeyDown you’ll need to convert it to a ‘Key’ type first – in fact, this is probably all your use of ScanCodeToRawKey is really achieving here – In this case, it doesn’t really matter whether you pass virtual keys or rawkeys or banana keys to KeyDown, since you’re just throwing every possible value at it!
To convert an int to a key, you can ‘Cast<Key>( n )’ it – example below. But as people have noted there are other ways to do this too.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )For Local i:=0 Until 512Local key:=Cast<Key>( i )If Keyboard.KeyDown( key ) Print "KeyDown:"+Keyboard.KeyName( key )EndEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndNovember 9, 2017 at 8:36 am #11591Thanks again to all and I hadn’t even thought about casting. Such a simple change and totally negated using ScanCodeToRawKey.
Shows how little I know the language.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.