In this article we are going to see how to check if a physical keyboard is deployed or not in a Windows phone device. Physical keyboard can be attached or detached from the device, which we can check with the help of DeviceStatus class which has some properties to check the availability of Physical device deployed or not. Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. [more]
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Add some controls to trigger the event to check if the Physical device is been deployed or not, basically we will add a Button control to trigger the click event and a textblock to display the information about the availability as shown in the code below.
XAML Code:
[code:c#]<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Photo Chooser" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Check Physical Keyboard" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" />
<TextBlock Height="334" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
</Grid>
In the button click event we need to check for the DeviceStatus class property (IsKeyboardpresent, IsKeyboarddeployed) to see if the physical keyboard is present and deployed to the device. Since its just a property we can get the information directly to a textblock in this sample application as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto66.Resources;
using Microsoft.Phone.Info;
namespace F5debugHowto66
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
txtoutput.Text = "Is Keyboard Present = " + DeviceStatus.IsKeyboardPresent.ToString() + "\n\r" + "Is Keyboard Deployed" +
DeviceStatus.IsKeyboardDeployed.ToString();
}
}
}
Now we are done with the code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screen below.
No Comments
whoah this weblog is excellent i really like reading your articles. Keep up the good work! You already know, a lot of persons are looking round for this information, you could help them greatly.