Raspberry Pi Zero W: Into a Wireless USB Keyboard (HID). Part 2 Python Typing Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there so welcome back to the second part of the video uh in the second part uh we are going to go into the the python typing script uh so we're just gonna make a we're gonna write the python script right now and this is uh i'm doing this specifically for the raspberry pi zero w i'm sure it'll work with other raspberry pi's but you know i tested this out on the raspberry pi zero so i can tell it it works so i'm turning into a wireless usb keyboard um and we call here hid hid stands for human interface device um and yeah that's something i thought you that's me want to know that so let's get into the code right now okay let me just close this and uh yeah so the very first thing you want to do is you want to set your interpreter um and this is this is beneficial it's uh this is beneficial if you're working in a virtual environment but even if you're not in working in in a virtual environment it doesn't hurt to have this line of code in your script so it's shabang which is a sharp there's a number sign and an exclamation point it's called shebang and i believe it is user usr that's short for user bin that's a binary in that's what it means and then enb is environment and we are using the python python 3 more specifically so we set our interpreter and the first thing we want is we're going to make a global character a global variable and i'm going to name this um global variable null character and it's just going to receive a character of zero so uh this but this uh what this function does it actually returns the the unicode for zero and i think it's like a special character or something there's like 255 characters uh so they're all unique um so that's what this does right here i'm using this null character and giving it giving it this this um assigning to this to character zero and uh you will you'll see why in a second so let's start with our first function our first function we're going to call it transcribe and it's going to receive a key like abcd any key on your on your on your keyboard it's going to receive a key here oh yeah but before we do that we can we can't i can't jump into this just said there's one part i need to do is use pass here just so it's um so i can come back to it later there's one part we need to do is we need to set up a code we're going to need to open a file and write to it and it's sort of essential for this whole thing to work this whole project to report to work so i'm going to make a new function called write report here write report and it's going to receive report that variable report and um we're going to use a try and accept script because we don't want to program so if it doesn't exist if the directory doesn't exist it won't crash so we need to try and accept uh function and we're just going to use pass for this accept because uh you know you just the whole point of this try accepted so it doesn't crash if something bad happens if something wrong happen you can make a mistake somewhere so i'm going to use with open and um the directory we're going to make in this directory exists so sometimes they exist and sometimes it doesn't exist if it doesn't exist then the destroy accept will allow the program to run regardless so it's open d e v h i d g is that right yes that is right we are going to open open this up in uh reading reading but reading bytes plus and so we're all booking this in reading and reading mode and more specifically reading in bytes mode and this plus means it allows you to read or write to this file and we're just going to name the this fd as as fd with and inside here which you can go fd right so we're writing to this directory here we're writing to this his hi um this this file here we're gonna write to it and inside we're going to write down report uh dot encode there we go so for this whole uh script of work you need this function here and we're using the try and accept so if this does not exist for any reason it's not going to crash it's going to keep on going and i noticed this makes a big difference in this try and accept in whether or not it works or not i just notice i always get good results when i when i started using this try and accept at this point here okay so now to the next part of the code we're gonna transcribe uh but we are gonna need a dictionary of um of characters so i have i have this uppercase and lowercase and i'm going to paste this in the description of the video so you guys can do the same thing there's no point in you guys um doing this by hand when using copy and paste it from this from the description of this video so here i'm going to stick these these dictionaries here all these dictionaries entire dictionary here and uh what key is let's say keys a b c d or or number so if a is inside here it's going to return four uh when we invoke the diction in the dictionary so let's erase this passage because we don't need any more and down here we've got an if statement so if key which is which is any key stroke on your computer in lower case so if it's inside if it's inside this dictionary so if basically if it's inside is basically if this is true if it's inside this dictionary then we want you to um use right report bracket and in this case we're using the lowercase we're going to use no character and you want to use this twice because times two no character twice plus c h or c um h r and for now i'm just gonna use i'm gonna leave it empty but we're gonna put something inside here in a second but just for a moment we're just gonna write the rest of this code out here so you can see how it looks like null character times five so you have a whole bunch of uh you have a whole bunch of these um ch uh this one these are characters zeros here and a whole bunch you have two of them here and two of them at the end or five of them at the end and in between here and in between here we need to stick one of these um these these the numbers inside here to get the letter we want right so um just to illustrate the points well we're well i'll tell you what we're putting inside here is specifically um we're going to be putting inside here lower case and then bracket key and the reason we're putting this inside here is well let me show you here in this empty in the sub this file so let's say a as the stroke let's say a equals i don't know that's what that's not the key story it's just a variable let's say a equals hello right so let's say a equals hello and if i did this if i did um print a and then brackets and i put a number here let's say uh zero right it's going to give me the first character h so let me just do that so print that and then i'm going to put a comma here and then put a bracket and put one so you're going to get an h and you're going to get an e so let's run this code you'll see it here at the bottom you'll be h and e beside each other see h and e right so that's what we um so that's when we uh we do it like this is it is a dictionary so let's say you have uh so let's say it was h here then it's gonna put that um well that's that's basically how these oh let me just let me show you how a dictionary works so let's say um let's say it's a or let's say a actually translate to four right so let's say a has uh we do a4 inside here and if you print it out it should well it gives you both because keystroke of that girl um no no no no sorry if it was if there was an a inside here so if there was a a that should give you four i believe okay well it's not a capital a should be a small small case a there we go there you go gives you four right so um over here that's what we do we made a dictionary here so if uh if a was entered for the key here it's gonna it's gonna end it's gonna give me character 4 right here now for the uppercase i use an else statement here but i like to do more i like to be specific so i'm using alif here so if if key is in uppercase uppercase then it's going to look a little different it's going to be same code it's going to be the exact same code so if key is inside here any of these then it's gonna it's gonna write this but we're gonna change this partner code we're gonna get rid of this to the part two and we have it just as a one i'm gonna put a plus there and we can use character and thirty two so what this uh what this does when you put down character 32 in just one null here it actually this is actually a shift key so you're actually clicking on the shift key holding the shift key down and then you're getting your the key you want which is a b or whatever over here and then it's going to follow the rest of this code here and that's going to give you a capitalized letter and then once all that is done you want to be able because right now you're pressing the key but you're not releasing the key right so you want to be able to release the key so the last part of the code we're going to do right report and then i'm going to give it five no characters no characters and then you can do five of them there and this should allow you to release the key and i believe that's the end of the code uh so that will give you the keystrokes and then the next part of the video we're gonna go we're gonna go a little bit further into words okay and that's about it
Info
Channel: ZeroLife
Views: 2,286
Rating: undefined out of 5
Keywords:
Id: GSIKft286dE
Channel Id: undefined
Length: 11min 28sec (688 seconds)
Published: Tue Dec 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.