Console App Question

Hi,

Is there a way to “clear” the console terminal while your console app is running? I am collecting a username/password when the console app fires up and I would like to clear it after I run it through my hash. Is there a method to clear the terminal?

Thank you!
Mike

But I don’t think you can clear the scroll back buffer anyway, so perhaps you need to rethink what you’re doing or how you’re doing it.

That assumes you know what kind of terminal it’s emulating to know what escape or control sequence to send

Sounds to me like you need a way to read the keyboard without showing the keystroke and I’m not sure you can do that directly in Xojo

Is this on windows ? If so, this link shows console clearing by fillling the buffer with spaces - but using the API from C

http://msdn.microsoft.com/en-us/library/ms682022(v=vs.85).aspx

If this is what you want, then it should be possible from RS/Xojo via declares

Also, a rather simpler bit of Windows API work… To suppress echo whilst password is typed, you can use a function like…

[code]function mySetConsoleMode ( bEchoState as boolean ) as boolean

// from example code at…
// http://msdn.microsoft.com/en-us/library/ms685035(v=vs.85).aspx

const Win32API_STD_INPUT_HANDLE = -10
const Win32API_ENABLE_ECHO_INPUT = 4
const Win32API_ENABLE_ECHO_INPUT_off_mask = &hFFFFFFFB
const Win32API_INVALID_HANDLE_VALUE = -1

soft declare function Win32API_GetStdHandle lib “Kernel32.dll” alias “GetStdHandle” ( byval nStdHandle as int32 ) as int32
soft declare function Win32API_GetConsoleMode lib “Kernel32.dll” alias “GetConsoleMode” ( byval nStdHandle as int32, byref dwMode as int32 ) as int32
soft declare function Win32API_SetConsoleMode lib “Kernel32.dll” alias “SetConsoleMode” ( byval nStdHandle as int32, byval dwMode as int32 ) as int32

// Keep old mode in a static variable…
static fdwSaveOldMode as int32

dim hStdIn as int32
dim dwNewMode as int32
dim i32Result as int32

// Get the standard input handle.
hStdIn = Win32API_GetStdHandle( Win32API_STD_INPUT_HANDLE )
if hStdIn = Win32API_INVALID_HANDLE_VALUE then
print “Failed to get handle”
RETURN ( false )
end if

// Set new echo state…
if bEchoState then

dwNewMode = fdwSaveOldMode

else

// Save the current input mode,  restored by resetting echo to ON
i32Result = Win32API_GetConsoleMode( hStdIn, fdwSaveOldMode )
if i32Result <> 1 then
  print "Failed to get mode: "  + str( i32Result )
  RETURN ( false )
end if

dwNewMode = fdwSaveOldMode AND Win32API_ENABLE_ECHO_INPUT_off_mask

end if

// Set this mode…
i32Result = Win32API_SetConsoleMode( hStdIn, dwNewMode )
if i32Result <> 1 then
print "Failed to set mode: " + str( i32Result )
RETURN ( false )
end if

RETURN ( true )[/code]

Then call it like:-

[code]dim strPwd as string
dim bResult as boolean

print “Try with no mods”
print “Enter password:”

strPwd = input
print " pwd=" + strPwd

print “Try with NO ECHO”
print “Enter password:”

bResult = mySetConsoleMode( false )

strPwd = input
print " pwd=" + strPwd

bResult = mySetConsoleMode( true )
[/code]

This works OK in my test on windows XP just now.

Maybe sending a ANSI/VT100 to clear the screen would be enough.

Erase Screen [2J
Erases the screen with the background colour and moves the cursor to home.

ANSI/VT100 commands

I don’t think windows Win32 console responds to those, but I did try some VT100 and VT52 sequences with no success.

I have not tried on other platforms though.

Thanks everyone. I will try those and my console app will be for Mac, Linux , and win. Thanks!!