Q: Automatically press a specific keyboard key in MATLAB Is there a way to get MATLAB to automatically press a specific keyboard key?
My internet connection disconnects without no reason. I want to make a script in MATLAB which check the internet connection status periodically and if the connection is disconnected the program will run the connection automatically.
I want to write a script in MATLAB which automatically run a connection (first picture). Then program should automatically press Enter key ("Connect" button in the first picture).  After this the second window will appear(second picture). In the second window if I click "connect" button, then the connection will connect to the internet. 
Here is my script:
function [] = AutoConn()
tic ;

cTime = 2 ;
k = 0 ;
A = 0 ;
while(1)
    nTime = toc ;
    tic ;
    C = isnett ;
    if C == 1
        A = 1 ;
    else
        A = 0 ;
        system('C:\...\Connection.pbk');
      {....}

    end
    clc;
    k = k+1;
    k
    A
    pause(cTime) ;
end


function flag = isnett()
% This function returns a 1 if basic internet connectivity
% is present and returns a zero if no internet connectivity
% is detected.

% define the URL for US Naval Observatory Time page
url =java.net.URL('http://tycho.usno.navy.mil/cgi-bin/timer.pl');

% read the URL
try
    link = openStream(url);
    parse = java.io.InputStreamReader(link);
    snip = java.io.BufferedReader(parse);
catch
    flag = 0;
    return
end
if ~isempty(snip)
    flag = 1;
else
    flag = 0;
end

First Picture:
               http://i.stack.imgur.com/OEy6W.png
Second Picture:
               http://i.stack.imgur.com/TPlEN.png

A: Yes, although undocumented, Matlab can do that by using java.awt.Robot, see Undocumented Matlab post on this topic.
It mentions a very nice FEX that facilitates the job for you: inputEmu.
Using inputEmu all you need to do is:
inputemu('key_normal','\ENTER');

Note: You will need to test if AWT is available:
error(javachk('awt'))

or
if usejava('awt')
   myFrame = java.awt.Frame;
else
   disp('Unable to open a Java Frame.');
end

