By implementing the KeyListener interface or extending the KeyAdapter.
I think the keyPressed event fires until it reaches the keyReleased, and if it doesn't continually fire it doesn't matter since you can simply use KeyPressed as a start, and keyReleased as an end. Tie that in with a variable to listen to them, or using a thread and it can constantly update the variable for you.
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php