Code for simulating a keypress

Hi,
I was playing with Bethon (python implementation of C++ BeOS API), and I needed to postmessage a keypress to a TextView.

basicly the idea was to recreate the BMessage (I know that there are system bmessages and an “in-app” bmessages which you can detect with the IsSystem() function), structured like this:

  		endkpress=BMessage(END_DOWN_MSG)
  		now=uptime._uptime_beos()
  		now=now*1000000
  		endkpress.AddInt64('when',long(now))
  		endkpress.AddInt32('key',60)
  		endkpress.AddInt32('modifiers',32)
  		endkpress.AddInt8('states',0)
  		endkpress.AddInt8('byte',97)
  		endkpress.AddString('bytes',"a")
  		endkpress.AddInt32('raw_char',97)
  		BApplication.be_app.PostMessage(endkpress)    #eventually WindowAt(0).
  		
  		endkpress=BMessage(END_UP_MSG)
  		now=uptime._uptime_beos()
  		now=now*1000000
  		endkpress.AddInt64('when',long(now))
  		endkpress.AddInt32('key',60)
  		endkpress.AddInt32('modifiers',32)
  		endkpress.AddInt8('states',0)
  		endkpress.AddInt8('byte',97)
  		endkpress.AddString('bytes',"a")
  		endkpress.AddInt32('raw_char',97)
  		BApplication.be_app.PostMessage(endkpress)  #eventually WindowAt(0).

as it seems it’s not working if you know, is there an alternative solution?

You can do something like this

void StrokeItView::dokeyevent( key_event_t event )
{
// this is an extremely truncated version os a routine lifted from VNC Server/SKBD

  BMessage msg;

		int32	curr_modifiers=0;	// modifiers
	
		if ( event.down )
			msg.what = B_KEY_DOWN;
		else
			msg.what = B_KEY_UP;
		
		char string[4] = {0,0,0,0};
    
  switch (event.key)
    {  
    case 13:
      msg.AddInt32("key", 0x47);
			  string[0] = B_RETURN;
      break;
    
    /*case ???: /////// DELETE as in char to left, not BACKSPACE
      msg.AddInt32("key", 0x34);
				string[0] = B_DELETE;
      break;*/
    case 8:
      msg.AddInt32("key", 0x1e);
				string[0] = B_BACKSPACE;
      break;
      
    default:
      //msg.AddInt32("key", event.key); // this has an odd effect in terminal
      string[0] = event.key;        
		}
  
  msg.AddString("bytes", string);
	  for ( int i=0; string[i]; i++ )
		  msg.AddInt8("byte", string[i]);
		msg.AddInt32("modifiers", curr_modifiers);

		#ifdef KEYBOARD_DEBUG
		msg.PrintToStream();
		#endif
		
		if ( msg.FlattenedSize() > 20 )
		{ // flatten and send if 
      
      input_port = find_port("StrokeIT Input port");
		  if ( input_port < 0 )
			  cout << "Error connecting to input server add-on" << endl;
		  //else
			//  cout << "Input server add-on port id: " << input_port << "\n";

			char * buffer = new char[msg.FlattenedSize()];
			msg.Flatten( buffer, msg.FlattenedSize() );

      if (  write_port( input_port, 123, buffer, msg.FlattenedSize() ) != B_OK  )
      { 
				cout << "Error writing to input add-on port\n";
      } 
			delete [] buffer;
		}
   
}

Mh… I’m trying to understand the C++ code ^_^’… Am I wrong if I say that the code reads the input and modifies the output?

Sorry - I was on my phone and missed off this part:

You need an input_server add-on to receive the data and post it to the input_server… well, at least this code works. I don’t know if it it the only way.

ah ok :slight_smile: Now I understand the steps needed to simulate a keypress
thank you

I don’t know if there is a more modern way, but an InputServer add-on used to be the only/best way.