Jump to content

Key Events in GameProject


Recommended Posts

Hi I am trying to get my app to respond to arrow keys in a GameProject. I used the code from the tetris sample.

 

However, it doesn't work. The code doesn't even appear to fire. What am I missing. The tetris code also does not appear to function.

 

 
procedure TApplication.ApplicationStarting; 
begin 
  inherited; 
  //Initialize refresh interval, set this to 1 for optimal speed 
  GameView.Delay:=2; 
  LastKey := 65; 

  MyX := 60; 
  myY := 90; 

  GameView.OnKeyDown := KeyDownEvent; 
  GameView.OnKeyUp := KeyDownEvent; 
  GameView.OnKeyPress := KeyPressEvent; 
  GameView.OnMouseDown := MouseDownEvent; 
  //Start the redraw-cycle with framecounter active 
  //Note: the framecounter impacts rendering speed. Disable 
  //the framerate for maximum speed (false) 
  GameView.StartSession(true); 
end; 

procedure TApplication.KeyDownEvent(Sender:TObject; AKeyCode: Integer); 
begin 
  LastKey := AKeyCode; 
  case AKeyCode of 
    37: dec(MyX); //FTetris.Left; 
    38: inc(MyX); //FTetris.Right; 
    39: inc(MyY); //up ? 
    40: dec(MyY); //down? 
  end; 
end;

 

I print out LastKey but it never changes

Link to post
Share on other sites

I did some javscript hacking to achive this. I dont know if there is also an other method but this works:

 

In ApplicationStarting:

 
asm 
  window.onkeydown=function(e) 
  { 
    TApplication.KeyDownEvent(Self,e.keyCode); 
  } 
  window.onkeyup=function(e) 
  { 
    TApplication.KeyUpEvent(Self,e.keyCode); 
  } 
end; 

KeyDownEvent(0);  //This is neede so that the optimizer does not remove the functions 
KeyUpEvent(0); 

 

The keyhandlers:

 
procedure TApplication.KeyDownEvent(mCode:integer); 
begin 
  if mCode = Config.Keys.Left then Player.GoLeft; 
  if mCode = Config.Keys.Right then Player.GoRight; 
end; 

procedure TApplication.KeyUpEvent(mCode:integer); 
begin 
  if mCode = Config.Keys.Left then Player.LeftUp; 
  if mCode = Config.Keys.Right then Player.RightUp; 
end; 

Link to post
Share on other sites

The last code is essentially right, but it will fail if you activate obfuscation. I wrote an article about hooking the window keyboard.

 

The reason the event doesnt fire, depends on the TAG used by the component. The TAG is created when you create an instance of the component class. In our case we use DIV as the root element, and div does not trigger the keyboard events. They only trigger on input controls like editbox etc..

 

To bypass this you can do, very simple, something like this:

 

 
procedure TApplication.HookKeyboard; 
var 
  mWindow: THandle; 
begin 
  mWindow:=BrowserAPI.GetWindow; 
  mWindow.onkeydown := @HandleKeyDown; 
end; 

 

Or, follow the article in "how to" (see documentation root link)

 

http://smartmobilestudio.com/2012/04/03/keyboard-events-how-to/

 

It is important NEVER to mix Smart properties and JS properties (!). Always use the RTL functions to hook objects, styles and events. This will shield you from future revisions and changes.

 

If you know the DOM well it is much better to reference it directly via a variant, like THandle. Just remember that Smart does not check that a property or method exist - but treats such calls a bit like IUnknown + IInvoke under delphi.

 

So if you have:

 

 
procedure TForm1.MyCallTest; 
var 
  mHandle:THandle; 
begin 
  asm 
  mHandle=window.someobject; 
  end; 
  mHandle.somesubobject.somemethod(); 
end; 

 

Smart wont check if "somesubobject" is actually there.

But it's a neat way of accessing the DOM directly if you know what you are doing and know the object you want to work with.

 

The BrowserAPI function returns the current "driver" for the browser you are using, and it has standard methods for hooking the window, document etc. elements.

The drivers will be greatly expanded shortly, but they are the best way of dealing with the top-level browser objects.

 

Things like phonegap will be seamlessly supported. If the app notice that it's running under phonegap -- it will use their API instead which is faster and has less security restrictions.

Link to post
Share on other sites

You lost me after "To bypass this you can do, very simple, something like this:"

 

I tried the code I tried using different handlers but they all come up with a handler mismatch error. Sorry, but my programming experience is quite narrow compared to most people.

 

I had tried the http://smartmobilestudio.com/2012/04/03/keyboard-events-how-to/ ideas but they don't seem to work in a TW3CustomGameApplication. That is one thing I hope you guys can work on, expand the basic samples. At the moment I am finding many are too complicated to learn from or don't cover issues that I am dealing with. Anyway, that wasn't a critism just an observation. I will try Jascha's code.

 

 
procedure TApplication.HandleKeyDown(Sender:TObject;aKeyCode:Integer); 
begin 
  writeln(aKeyCode); 
end; 

//############################################################################ 
// TApplication 
//############################################################################ 

procedure TApplication.HookKeyboard; 
var 
mWindow: THandle; 
Begin 
mWindow:=BrowserAPI.GetWindow; 
mWindow.onkeydown:=@HandleKeyDown; 
end; 

Link to post
Share on other sites

I sorted my own problem. I have used Jascha code for time being replaced config.keys with the key values 37, 38, 39 etc

 

Instead of hot fixes have you considered making daily builds available? Especially in teh beginning while there are many bugs and issues reported?

 

It is a long time between hot fixes if we have to wait for critical updates.

Link to post
Share on other sites
  • 5 years later...

Hi Guys,

 

I am currently working on porting a Delphi game to SMS and I ran into the same problem that I ran into five years ago.

 

Namely the gameview.onkeydown/up/press events don't work.

 

 

I have used Jascha code again to solve the problem but I am hoping that this issue could be resolved in the upcoming RTL update as it is very confusing to have a seemingly obvious way to handle keyboard input and yet it doesn't work.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...