Combining arduino leonardo code with water alarm code
How do you want them to work ? Do you want the spacebar pressed when there is a high signal read (when the light goes on)?

If you want the space bar press when it goes high this might work:

#include &lt;Keyboard.h&gt;

bool eenkeergeweest = false;
int out = 12;
int in = 4;

void toggleKeyPress (const char&amp; keypress)
{
    if (!eenkeergeweest) 
    {
        eenkeergeweest = true;
        Keyboard.print(' ');
        delay(100);
    }
}

void setup()
{
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);

    // initialize control over the keyboard:
    Keyboard.begin();

}


void loop()
{
    const int rd = digitalRead(in);
    if (rd == HIGH)
    {
        toggleKeyPress(' ');
        digitalWrite(out, HIGH);
        // wait for new window to open:
        delay(1000);   
    }
    else
    {
        digitalWrite(out, LOW);
    }
}