Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Additional snippets
Adds snippets for Console related types

* ConsoleCancelEventargs
* ConsoleCancelEventHandler
* ConsoleColor
* ConsoleKey
* ConsoleKeyInfo
* ConsoleModifiers
* ConsoleSpecialKey
  • Loading branch information
albert-du committed Jan 1, 2022
commit 45d11ec7f46475a1781fad0c1aee5f2d00ed8880
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// <Snippet1>
open System
open System.Text

let mutable input = Unchecked.defaultof<ConsoleKeyInfo>

while input.Key <> ConsoleKey.Escape do
printfn "Press a key, together with Alt, Ctrl, or Shift."
printfn "Press Esc to exit."
input <- Console.ReadKey true

let output = StringBuilder $"You pressed {input.Key}"
let mutable modifiers = false

if input.Modifiers &&& ConsoleModifiers.Alt = ConsoleModifiers.Alt then
output.Append ", together with {ConsoleModifiers.Alt}" |> ignore
modifiers <- true

if input.Modifiers &&& ConsoleModifiers.Control = ConsoleModifiers.Control then
if modifiers then
output.Append " and " |> ignore
else
output.Append ", together with " |> ignore
modifiers <- true
output.Append(string ConsoleModifiers.Control) |> ignore

if input.Modifiers &&& ConsoleModifiers.Shift = ConsoleModifiers.Shift then
if modifiers then
output.Append " and " |> ignore
else
output.Append ", together with " |> ignore
modifiers <- true
output.Append(string ConsoleModifiers.Shift) |> ignore
output.Append "." |> ignore

printfn $"{output}\n"


// The output from a sample console session might appear as follows:
// Press a key, along with Alt, Ctrl, or Shift.
// Press Esc to exit.
// You pressed D.
//
// Press a key, along with Alt, Ctrl, or Shift.
// Press Esc to exit.
// You pressed X, along with Shift.
//
// Press a key, along with Alt, Ctrl, or Shift.
// Press Esc to exit.
// You pressed L, along with Control and Shift.
//
// Press a key, along with Alt, Ctrl, or Shift.
// Press Esc to exit.
// You pressed P, along with Alt and Control and Shift.
//
// Press a key, along with Alt, Ctrl, or Shift.
// Press Esc to exit.
// You pressed Escape.
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ConsoleKey1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//<snippet1>
// This example demonstrates the ConsoleKeyInfo.Equals() method.

open System
open System.Text

// The keyCombination function creates a string that specifies what
// key and what combination of shift, CTRL, and ALT modifier keys
// were pressed simultaneously.
let keyCombination (sourceCki: ConsoleKeyInfo) =
let sb = StringBuilder()
sb.Length <- 0
if int sourceCki.Modifiers <> 0 then
if int (sourceCki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then
sb.Append "ALT+" |> ignore
if int (sourceCki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then
sb.Append "SHIFT+" |> ignore
if int (sourceCki.Modifiers &&& ConsoleModifiers.Control) <> 0 then
sb.Append "CTL+" |> ignore

sourceCki.Key
|> string
|> sb.Append
|> string

//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.
//
Console.TreatControlCAsInput <- true

let mutable cki1 = Unchecked.defaultof<ConsoleKeyInfo>
let mutable cki2 = Unchecked.defaultof<ConsoleKeyInfo>

// Request that the user enter two key presses. A key press and any
// combination shift, CTRL, and ALT modifier keys is permitted.
while cki1.Key <> ConsoleKey.Escape do
printf "\nEnter a key ......... "
cki1 <- Console.ReadKey false
printf "\nEnter another key ... "
cki2 <- Console.ReadKey false
printfn ""

let key1 = keyCombination cki1
let key2 = keyCombination cki2
let equalValue =
if cki1.Equals cki2 then ""
else "not "

printfn $"The {key1} and {key2} keys are {equalValue}equal."

printfn "Press the escape key (ESC) to quit, or any other key to continue."
cki1 <- Console.ReadKey true

// Note: This example requires the Escape (Esc) key.


// This example produces results similar to the following output:
//
// Enter a key ......... a
// Enter another key ... a
// The A and A keys are equal.
// Press the escape key (ESC) to quit, or any other key to continue.
//
// Enter a key ......... a
// Enter another key ... A
// The A and SHIFT+A keys are not equal.
// Press the escape key (ESC) to quit, or any other key to continue.
//
// Enter a key ......... S
// Enter another key ...
// The ALT+SHIFT+S and ALT+CTL+F keys are not equal.
// Press the escape key (ESC) to quit, or any other key to continue.
//
// Enter a key .........
// Enter another key ...
// The UpArrow and UpArrow keys are equal.
// Press the escape key (ESC) to quit, or any other key to continue.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="equals.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="hash.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//<snippet1>
// This example demonstrates the ConsoleKeyInfo.GetHashCode() method.

open System
open System.Text

// The keyCombination function creates a string that specifies what
// key and what combination of shift, CTRL, and ALT modifier keys
// were pressed simultaneously.
let keyCombination (sourceCki: ConsoleKeyInfo) =
let sb = StringBuilder()
sb.Length <- 0
if int sourceCki.Modifiers <> 0 then
if int (sourceCki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then
sb.Append "ALT+" |> ignore
if int (sourceCki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then
sb.Append "SHIFT+" |> ignore
if int (sourceCki.Modifiers &&& ConsoleModifiers.Control) <> 0 then
sb.Append "CTL+" |> ignore

sourceCki.Key
|> string
|> sb.Append
|> string

//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.
//
Console.TreatControlCAsInput <- true

let mutable cki1 = Unchecked.defaultof<ConsoleKeyInfo>

// Request that the user enter two key presses. A key press and any
// combination shift, CTRL, and ALT modifier keys is permitted.
while cki1.Key <> ConsoleKey.Escape do
printf "\nEnter a key ......... "
cki1 <- Console.ReadKey false
printfn ""

let key1 = keyCombination cki1
let hashCode = cki1.GetHashCode()
printfn $"The hash code for the {key1} key is {hashCode}."

printfn "Press the escape key (ESC) to quit, or any other key to continue."
cki1 <- Console.ReadKey true

// Note: This example requires the Escape (Esc) key.
//
// This example produces results similar to the following output:
//
// Enter a key ......... a
// The hash code for the A key is 97.
// Press the escape key (ESC) to quit, or any other key to continue.
//
// Enter a key ......... S
// The hash code for the SHIFT+S key is 83.
// Press the escape key (ESC) to quit, or any other key to continue.
//
// Enter a key .........
// The hash code for the ALT+SHIFT+CTL+J key is 7.
// Press the escape key (ESC) to quit, or any other key to continue.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="keychar1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <Snippet1>
open System

// Configure console.
Console.TreatControlCAsInput <- true

printfn "Enter a string. Press <Enter> or Esc to exit."

let mutable inputString = String.Empty
let mutable keyInfo = Unchecked.defaultof<ConsoleKeyInfo>

while keyInfo.Key <> ConsoleKey.Enter && keyInfo.Key <> ConsoleKey.Escape do
keyInfo <- Console.ReadKey true

// Ignore if Alt or Ctrl is pressed.
if keyInfo.Modifiers &&& ConsoleModifiers.Alt <> ConsoleModifiers.Alt &&
keyInfo.Modifiers &&& ConsoleModifiers.Control <> ConsoleModifiers.Control &&
// Ignore if KeyChar value is \u0000.
keyInfo.KeyChar <> '\u0000' &&
// Ignore tab key.
keyInfo.Key <> ConsoleKey.Tab then

// Handle backspace.
if keyInfo.Key = ConsoleKey.Backspace then
// Are there any characters to erase?
if inputString.Length >= 1 then
// Determine where we are in the console buffer.
let cursorCol = Console.CursorLeft - 1
let oldLength = inputString.Length
let extraRows = oldLength / 80

inputString <- inputString.Substring(0, oldLength - 1)
Console.CursorLeft <- 0
Console.CursorTop <- Console.CursorTop - extraRows
printf $"{inputString + String(' ', oldLength - inputString.Length)}"
Console.CursorLeft <- cursorCol
else
// Handle key by adding it to input string.
printf $"{keyInfo.KeyChar}"
inputString <- inputString + string keyInfo.KeyChar

printfn $"""

You entered:
{if String.IsNullOrEmpty inputString then "<nothing>" else inputString}"""
// </Snippet1>
3 changes: 3 additions & 0 deletions xml/System/ConsoleCancelEventArgs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.cancelkeypress/cpp/ckp.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.cancelkeypress/cs/ckp.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.cancelkeypress/fs/ckp.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.cancelkeypress/vb/ckp.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -111,6 +112,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.cancelkeypress/cpp/ckp.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.cancelkeypress/cs/ckp.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.cancelkeypress/fs/ckp.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.cancelkeypress/vb/ckp.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -166,6 +168,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.cancelkeypress/cpp/ckp.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.cancelkeypress/cs/ckp.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.cancelkeypress/fs/ckp.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.cancelkeypress/vb/ckp.vb" id="Snippet1":::

]]></format>
Expand Down
1 change: 1 addition & 0 deletions xml/System/ConsoleCancelEventHandler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.cancelkeypress/cpp/ckp.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.cancelkeypress/cs/ckp.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.cancelkeypress/fs/ckp.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.cancelkeypress/vb/ckp.vb" id="Snippet1":::

]]></format>
Expand Down
1 change: 1 addition & 0 deletions xml/System/ConsoleColor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
The following example saves the values of the <xref:System.ConsoleColor> enumeration to an array and stores the current values of the <xref:System.Console.BackgroundColor%2A> and <xref:System.Console.ForegroundColor%2A> properties to variables. It then changes the foreground color to each color in the <xref:System.ConsoleColor> enumeration except to the color that matches the current background, and it changes the background color to each color in the <xref:System.ConsoleColor> enumeration except to the color that matches the current foreground. (If the foreground color is the same as the background color, the text isn't visible.) Finally, it calls the <xref:System.Console.ResetColor%2A> method to restore the original console colors.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.consolecolor/cs/foregroundcolor3.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.consolecolor/fs/foregroundcolor3.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.consolecolor/vb/foregroundcolor3.vb" id="Snippet1":::

]]></format>
Expand Down
1 change: 1 addition & 0 deletions xml/System/ConsoleKey.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ConsoleKey/cpp/consolekey.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ConsoleKey/cs/ConsoleKey1.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ConsoleKey/fs/ConsoleKey1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ConsoleKey/vb/ConsoleKey1.vb" id="Snippet1":::

]]></format>
Expand Down
6 changes: 6 additions & 0 deletions xml/System/ConsoleKeyInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.readkey1/CPP/rk.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.readkey1/CS/rk.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.readkey1/FS/rk.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.readkey1/VB/rk.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -254,6 +255,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.Equals/cpp/consolekeyinfo.equals.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.Equals/cs/equals.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.Equals/fs/equals.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ConsoleKeyInfo.Equals/vb/equals.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -308,6 +310,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.GetHashcode/cpp/hash.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.GetHashcode/cs/hash.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ConsoleKeyInfo.GetHashcode/fs/hash.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ConsoleKeyInfo.GetHashcode/vb/hash.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -356,6 +359,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.readkey1/CPP/rk.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.readkey1/CS/rk.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.readkey1/FS/rk.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.readkey1/VB/rk.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -408,6 +412,7 @@
The following example uses the <xref:System.ConsoleKeyInfo.KeyChar%2A> property to add the characters input by the user into a string. The example ignores special keys other than **ENTER**, **ESC**, and **BACKSPACE**.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.consolekeyinfo.keychar/cs/keychar1.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.consolekeyinfo.keychar/fs/keychar1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.consolekeyinfo.keychar/vb/keychar1.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -456,6 +461,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/console.readkey1/CPP/rk.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/console.readkey1/CS/rk.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/console.readkey1/FS/rk.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/console.readkey1/VB/rk.vb" id="Snippet1":::

]]></format>
Expand Down
Loading