Hook CTRL+ALT+TAB - wpf

I've made a WPF application and I was able to hook some of the windows combinations. ALT+TAB is hooked and it is doing nothing when my application is running (as expected). The problem is when I press the CTRL+ALT+TAB I get the same effect as ALT+TAB.
Do you guys have any idea on how to hook this kind of combination?
EDIT:
I have already successfully hooked ALT+TAB. I do want to hook CTRL+ALT+TAB. I've tried this project example to make this happen.
Here's the code that makes the hook:
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam,
ref KBHookStruct lParam){
if (nCode == 0)
{
if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) || // Alt+Tab
((lParam.vkCode == 0x1B) && (lParam.flags == 0x20)) || // Alt+Esc
((lParam.vkCode == 0x1B) && (lParam.flags == 0x00)) || // Ctrl+Esc
((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key
((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) || // Right Windows Key
((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) || // Alt+F4
((lParam.vkCode == 0x20) && (lParam.flags == 0x20))) // Alt+Space
{
return new IntPtr(1);
}
}
return CallNextHookEx(hookPtr, nCode, wParam, ref lParam);}

Answer 1
You can try like below it work for CTRL + SHIFT + TAB and CTRL + TAB
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
MessageBox.Show("CTRL + SHIFT + TAB trapped");
}
if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
MessageBox.Show("CTRL + TAB trapped");
}
}
Answer 2
should look something like:
((lParam.flags & 33 == 33) && (lParam.flags & 22 == 22))
32 and 22 are arbitrary in this example. You need to figure out what values ALT and CTRL actually are. They will be 1, 2, 4 ... 16, 32 etc. so that they can be OR'ed together into a single value.

Related

How to use string variable as Table name in SQL query

Making a winforms application and I want the name of the table in my SQL query to be equal to a variable I have called TreeCodeName. This is my code:
private void button1_Click(object sender, EventArgs e)
{
string TreeNodeName = treeView1.SelectedNode.Name.ToString();
if ((TreeNodeName == "BOOKS") || (TreeNodeName == "CARDS") || (TreeNodeName == "COMPUTERS") || (TreeNodeName == "CONSOLES") || (TreeNodeName == "DEVICES") || (TreeNodeName == "DONORS") || (TreeNodeName == "MAGAZINES") || (TreeNodeName == "MOTHERBOARDS") || (TreeNodeName == "OTHERS") || (TreeNodeName == "PARTS") || (TreeNodeName == "PERIPHERALS") || (TreeNodeName == "POWER_SUPPLY") || (TreeNodeName == "PROCESSORS") || (TreeNodeName == "RAMS") || (TreeNodeName == "SOFTWARE") || (TreeNodeName == "STORAGE") || (TreeNodeName == "TERMINALS"))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * From Table Where Title = #Title";
command.Parameters.AddWithValue("#Title", TreeNodeName);
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
Whenever I try to test this, I run into an error that says there was a syntax error on FROM. Any way to salvage this?

ng-pattern for mobile number validation

I am trying to implement mobile number validation on a Visualforce page using Angular JS, but am having problems getting my head around how to write the regex expression.
The requirements, as given to me, are fairly simple:
The number should be 10 digits long (I've already set the maxlength attribute on the field so this one is kind of taken care of already)
It should start with 04 (as it is an Australian mobile)
Only numbers should be allowed.
The regex expression I am using (in the ng-pattern attribute for my phone number input field) is:
^/(04)[0-9]{10}/$
This works up to a point - it does not allow anything other than numbers and does let me enter a 10 digit number starting with 04. However it also lets me enter numbers starting with, for example, 02, 03 etc....
Probably quite a simple thing I'm missing but I've had a look at quite a few sites, including this one, and can't find the answer.
Any help would be hugely appreciated as this one has caused me a few grey hairs already.
Try using this pattern
Use this in ur HTML file:
<input type="text" (keypress)="keyPress($event)" minlength=10 maxlength=10>
Use this in ur JS file:
keyPress(event: any) {
const pattern = /[0-9\+\-\ ]/;
let inputChar = String.fromCharCode(event.charCode);
if (event.keyCode != 8 && !pattern.test(inputChar)) {
event.preventDefault();
}
}
Try this one:
Mobile Number :
//inside view:
<input type="text" class="form-control" ng-model="mobileNo" name="mobileNo" ng-pattern="regEx" />
//inside controller:
$scope.regEx="/^[0-9]{10,10}$/";
For starters I had to create a whole js function for it... and it does validates as you type. here is my full code I hope this can help you get where you need.
This function gets the string every time a key is beign pressed and it allows the carrete to move front, back, delete and backspace. check it out and let me know if it helps you. you can run it on any situation and this is how I would add the "04" validation
//phone validation 10 digits and parenthesis (344)567-0011
function validatePhone(inputId) {
let validKey = false;
const input = document.getElementById(inputId);
let enteredDigits = input.value;
//switch to remove the country code added by default on autoComplete forms.
if (enteredDigits.length > 10 && enteredDigits[0] == '+') {
switch (enteredDigits.length) {
case 12:
enteredDigits = enteredDigits.slice(2);
break;
case 13:
enteredDigits = enteredDigits.slice(3);
break;
case 14:
enteredDigits = enteredDigits.slice(4);
break;
default:
enteredDigits = enteredDigits.replace(/\D+/g, '');
}
}
let newPhone = enteredDigits.replace(/\D+/g, ''); // This replace any character that is not a number.
const key = event.keyCode || event.charCode; // Get the pressed key.
let caretPosition = input.selectionStart; // get the carret position.
// splits, removes the "-" and converts from array to string and gives the needed digits.
const areaCode = newPhone.split('').splice(0, 3).toString().replace(/,/g, '');
const threeDigit = newPhone.split('').splice(3, 3).toString().replace(/,/g, '');
const fourDigit = newPhone.split('').splice(6, 8).toString().replace(/,/g, '');
// Moving carret on different positions. when the numeric keys are being pressed.
// Key >= 48 && key <= 58 number keys.
// Key >= 96 && key <= 105 numeric path number keys.
if ((key >= 48 && key <= 58) || (key >= 96 && key <= 105)) {
validKey = true;
if (threeDigit.length > 0) {
if (caretPosition == 1) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 4 && newPhone.length == 4) {
caretPosition = caretPosition + 2;
} else if (caretPosition == 4 && newPhone.length >= 5) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 5) {
caretPosition = caretPosition + 1;
} else if (caretPosition >= 2 && caretPosition <= 3 && newPhone.length <= 4) {
caretPosition = caretPosition + 1;
}
}
if (fourDigit.length > 0 && caretPosition == 9) {
caretPosition = caretPosition + 1;
}
}
// Key = 8 = Backspace.
else if (key == 8) {
validKey = true;
if (caretPosition == 3 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 2 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 1 && newPhone.length == 3 && threeDigit.length == 0) {
caretPosition = caretPosition - 1;
}
}
// Key = 46 = Delete. Key =37 = ArrowLeft. Key = 39 = ArrowRight.
else if (key == 46 || key == 39 || key == 37) {
validKey = true;
// Delete
if (key == 46) {
if (caretPosition == 0 && newPhone.length > 3) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 1 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 2 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 3 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if ((newPhone.length >= 4 && caretPosition == 4) || (newPhone.length >= 4 && caretPosition == 8)) {
caretPosition = caretPosition + 1;
}
}
}
//here is the validation for the country that you need.
if ((newPhone.length == 1 && newPhone[0] != '0') || (newPhone.length >= 2 && newPhone[1] != '4')) {
alert('Must start with 04');
newPhone = '';
}
// Adding the special character for formatting.
if (threeDigit.length > 0 && fourDigit.length == 0) {
newPhone = '(' + areaCode + ')' + threeDigit;
} else if (fourDigit.length > 0 && threeDigit.length == 3) {
newPhone = '(' + areaCode + ')' + threeDigit + '-' + fourDigit;
}
if (!validKey) {
caretPosition = caretPosition - 1;
}
// Set new values.
newPhone = newPhone.substring(0, 13);
input.value = newPhone;
input.focus();
input.setSelectionRange(caretPosition, caretPosition);
}
<form name="myForm"
onsubmit="return validateForm()"
method="post">
Phone number: <input type="text"
id="phoneNumber"
name="fPhone"
onkeyup="validatePhone('phoneNumber')">
<input type="submit"
value="Submit">
</form>

How to create USER DEFINED EVENTS in ALLEGRO 5

I am working on a game in allegro 5 in which I want to create rectangular objects dynamically on screen and make them clickable with mouse buttons
al_register_event_source( event_queue, al_get_timer_event_source(timer));
al_register_event_source( event_queue, al_get_mouse_event_source());
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
al_start_timer(timer);
while ( !exit )
{
ALLEGRO_EVENT ev;
al_wait_for_event( event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
;
else if ( ev.type == ALLEGRO_EVENT_MOUSE_AXES )
{
x = ev.mouse.x;
y = ev.mouse.y;
}
else if ( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
{
if ( x >= rect.x && x <= rect.maxx && y >= rect.y && y <= rect.maxy )
destory ( rect );
}
else if ( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
break;
if ( redraw && al_event_queue_is_empty(event_queue)){
redraw = false;
al_draw_rectangle ( rect.x, rect.y, rect.maxx, rect.maxy, blue, 1 );
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
But this is hardcoded for only one rectangle. How can I make an event for this which can handle rectangles like button.
User events aren't needed to respond to button presses.
Rather, you should approach this from a different angle. Pass the ALLEGRO_EVENT to your button class and have it return whether it was clicked or not.
bool Button::ButtonPressed(ALLEGRO_EVENT ev) {
if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button == 1) {
if (our_area.Contains(ev.mouse.x , ev.mouse.y)) {return true;}
}
return false;
}
BUT, you asked how to create user events in Allegro 5 so I will answer that as well.
See https://liballeg.org/a5docs/trunk/events.html#allegro_user_event for details and a code example. Basically, you create and initialize an ALLEGRO_EVENT_SOURCE, register it with your event queue, and then listen for messages you emit with al_emit_user_event.

moving objects withe key - diagonal

I need to move an object, I can move it up down and sideways, but fails in a diagonal, it is the way I made my move.
if (e.Key == Key.Up && Canvas.GetTop(Good) > 31)
{
double top = Canvas.GetTop(Good);
Canvas.SetTop(Good, top - 7);
}
if (e.Key == Key.Down && Canvas.GetTop(Good) < CanvasA.ActualHeight - 7 - Good.Height)
{
double down = Canvas.GetTop(Good);
Canvas.SetTop(Good, down + 7);
}
if (e.Key == Key.Left && Canvas.GetLeft(Good) > -2)
{
double left = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, left - 7);
}
if (e.Key == Key.Right && Canvas.GetLeft(Good) < CanvasA.ActualWidth - Good.Width)
{
double right = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, right + 7);
}
}
}
Everything works but can not click on the two buttons to move the character in a diagonal,
The reason that there is less than 31, it's because I did Menu in WPF.

Silverlight NumericUpDown: Set max character length, or validate against it?

I'm attempting to use the key up event to hijack the event and prevent two things. Anything but numeric keys or delete/back as well as once they hit a certain max of character length, inhibit the entry.
This solves the first validation portion, but I can't seem to figure out a way to prevent entry past a certain character length.
private void numericFieldInputField_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 ||
e.Key == Key.D6 || e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 ||
e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 || e.Key == Key.Back || e.Key == Key.Delete ||
e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Tab)
{
e.Handled = true;
}
else
{
//show validation
}
}
When I check the length of the numeric updown's value, cast it to string and look at the length property at this point, the last character they entered won't register. Also, when I set e.Handled to true, it still enters the character into the input. I thought e.Handled = true would keep the UI from putting the character into the input?
I would suggest using a Validator. In the databinding you specify the validator you want, the framework takes care of the rest. Here's one good intro to the topic: Tutorial

Resources