Java中清空console控制檯的程式碼片段
阿新 • • 發佈:2018-12-16
該操作時讓機器人模擬人手按快捷鍵來實現的
void clear() { try { Robot r = new Robot(); r.mousePress(InputEvent.BUTTON3_DOWN_MASK); //按下滑鼠右鍵 r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); //釋放滑鼠右鍵 r.keyPress(KeyEvent.VK_CONTROL); //按下Ctrl鍵 r.keyPress(KeyEvent.VK_R); //按下R鍵 r.keyRelease(KeyEvent.VK_R); //釋放R鍵 r.keyRelease(KeyEvent.VK_CONTROL); //釋放Ctrl鍵 r.delay(100); } catch (AWTException e) { e.printStackTrace(); } }
然後import以下包: import java.io.*; import java.awt.Robot; import java.awt.AWTException; import java.awt.event.InputEvent; import java.awt.event.KeyEvent;