Glogal Wire Transmission disables Keyboard.write()

I am writing a keyboard switch matrix scanner that uses Wire Transmission() followed by Keyboard.write(). But then the Keyboard.write() does not work.
If I comment the Wire Transmission(), then Keyboard.write() works.
Global Matrix object m1 demonstrates the problem.
Matrix object m2 works, but it’s local scope is useless to the project.

keyboard and wire libraries are at:
http://arduino.cc/en/Reference/Wire
http://arduino.cc/en/Reference/MouseKeyboard

Here is the example sketch:

#include <Wire.h>
#include <Keyboard.h>

class Matrix
{
	public:
		Matrix();
		void scan();
};
Matrix::Matrix()
{
	Wire.begin();
 	Wire.beginTransmission(0x20);	// when these two Wire..Transmission() are commented,
	Wire.endTransmission();		// the outputs are as expected
}
void Matrix::scan() // scan the keyboard switch matrix and write the key's character
{
	Keyboard.write('S');
};
Matrix m1;		// instantiate global m1

void setup()
{
	delay(2000);
	Keyboard.begin();

	Matrix m2;	// instantiate local m2
	m2.scan();
	Keyboard.write('2');

}

void loop()
{
	m1.scan();
	Keyboard.write('1');

	while(1) { }
}

Here are the outputs depending on which Matrix objects are commented:

INSTANTIATE 	CHARACTERS WRITTEN	CHARACTERS I WAS EXPECTING
 m1 and m2	none			S2S1
 m1		none			S1
 m2		S21			S21
 neither	21			21

I get the same results if I substitute Keyboard.write with Keyboard.press(); Keyboard.release();
Is this a bug or am I doing something wrong?

First, Wire.begin() should be done once (in setup).

Second:

http://www.gammon.com.au/forum/?id=12153#trap20

Third, since you have two instances of Matrix, you are calling Wire.begin() twice.

Thanks Nick, it all makes sense now. I fell into Trap #20 headfirst: Doing too much in a constructor.

This sketch avoids the “static initialization order fiasco”:

#include <Wire.h>
#include <Keyboard.h>

class Matrix
{
	public:
		void begin();
		Matrix();
		void scan();
};
void Matrix::begin()
{
	Wire.begin();
 	Wire.beginTransmission(0x20);
	Wire.endTransmission();	
}
Matrix::Matrix()
{
}
void Matrix::scan() // scan the keyboard switch matrix and write the key's character
{
	Keyboard.write('S');
};
Matrix m1;		// instantiate global m1

void setup()
{
	m1.begin();
	Keyboard.begin();
	delay(2000);
}

void loop()
{
	m1.scan();
	Keyboard.write('1');

	while(1) { }
}